The destroy of the data with the foreign key is not successful, and even if you add the dependent option, you will get an error.

Asked 2 months ago, Updated 2 months ago, 1 views

When I delete a post model record, I want to delete a comment model record associated with it, but I get the following error.
Currently, if there is no comment, it can be deleted normally.

If anyone understands, please take care of me.

error messages:

Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails (`anipho_development`.`comments`, CONSTRINT`fk_rails_2fd19c0db7`FOREIGN KEY(`post_id`) REFERENCES````d``))

The current code looks like this

Migration Files

class CreateComments <ActiveRecord::Migration [6.0]
  def change
    create_table:comments do | t |
      t.string:content, null:false


class Comment <ApplicationRecord
  belongs_to —user
  belongs_to —post
  values:content, presence:true


class Post <ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash —category
  belongs_to —user
  has_one_attached —image

  with_options presence —true do
   values:category_id,numberality:{other_than:1,message:"Choose from something other than --"} 


class User<ApplicationRecord
  # Include default device modules.Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  device:database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many —posts
  has_many —Comments
  values:nickname, presence:true

ruby-on-rails mysql

2022-09-30 14:13

1 Answers

The cord looks like it's going to work properly.
How do you delete the post model?

There are several ways to delete a record, but Rails generally uses one of the four ActiveRecord methods.
delete, delete_all, destroy, destroy_all.
There are four things that may be confusing, but in order to delete them so that they don't get the error,


You need to erase it like this.

There are four deep-fried methods, delete and destory.

  • delete system... When executed, ActiveRecord generates SQL for the appropriate DELETE statement and executes it as it is.Any activities that ActiveRecord performs with updates, such as validation and callback, are skipped.
  • Destroy...Unlike above, ActiveRecord performs the delete action after performing the action that accompanies the update process.

The difference is

Defined in Post Model

 has_many:comments, dependent::destroy

This. This dependency definition is equivalent to a callback and will not be executed if deleted using the delete method.
Therefore, the following SQL will be issued as soon as the method is executed.

 DELETE FROM posts where id=xxxx

As the migration file in CreateComments defines the FK,


The records in the comments table are always configured to have the id of the records in the parent posts table.
If you delete the posts record with the comments left, the comments will float in orphaned state, so there is an FK error to prevent them.
To avoid this error, you must first erase the comments records, but to do so, you must use the destroy method as well as the definition of dependent::destroy.

2022-09-30 14:13

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.