Home>

Currently, we are creating a function that allows you to post multiple reviews for the item model.
So when I tried to delete an item with multiple reviews, I got the following error statement.

ActiveRecord :: InvalidForeignKey in ItemsController # destroy
PG :: ForeignKeyViolation: ERROR: update or delete on table "reviews" violates foreign key constraint "fk_rails_5350d1b47c" on table "comments" DETAIL: Key (id) = (6) is still referenced from table "comments".: DELETE FROM " reviews "WHERE" reviews "." id "= $1

While implementing the review function, I thought about making it possible to post multiple comments to review.
There is a history that I stopped implementing it because I thought it was unnecessary on the way.

Is there a comments table left? I tried to delete the table
Since it becomes uninitialized constant Comments, it can be deleted, but the foreign key remains? Kanato
I am thinking. If i make a mistake, please point it out.
I can't destroy_all from the console either.
How can I delete the item in this case? Please teach us.

item.rb
has_many: category_items, dependent:: destroy
has_many: categories, through :: category_items
has_many: reviews, dependent:: destroy
has_many: favorites, dependent:: destroy
accepts_nested_attributes_for: category_items
review.rb
belongs_to: user
belongs_to: item
has_many: notifications, dependent:: destroy
  • Answer # 1

    There is a comments table left in the database and there are still records there,
    I think that's what it means.

    Let's check the existence first
    rails db: schema: dump
    Will update db/schema.rb. In it
    create_table "comments" ,. .. .. .. ..
    If there is a line that says, there is a table.

    Let's see the state of migration
    rails db: migrate: status
    Please see. I think there is evidence of creating a Comment in it.
    There is no evidence that it was deleted, right?
    Then create a migration that removes the Comment and run it.
    Contents is like this

    def change
       drop_table (: comments)
    end