I was addicted to the record of the associated model

Work environment

Rails '5.2.3' Ruby '2.7.1' PostgreSQL

What I was addicted to

I tried to delete the registered item while checking the operation in the local environment while creating the review sharing application.

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

Error ,. There is no comment table called ʻon table" comments "`. I was worried, but when I first created the app, I was thinking of creating a comment function for review, but isn't it necessary to have a one-sided comment function for review? There was a past that I stopped implementing it because I thought (I forgot). However, I thought that the comments table was deleted and I was addicted to it.

Addictive background

The relationship is that an item has multiple reviews while creating a review sharing app in Rails.

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

Think about what caused the error

From here, it is a consideration that a beginner thought. I would appreciate it if you could point out if it is different. At the same time you try to delete the item, the review associated with the item will also be deleted. has_many: reviews, dependent:: destroy This. So this time, item is deleted → review disappears → comment table remains

review.rb


(abridgement)
has_many  :comments, dependent: :destroy

I thought that it was a phenomenon that review could not be erased because there was no description, so item could not be erased.

So how to deal with this error

First of all, it is a confirmation that the comments table really remains. Updated db / schema.rb with rails db: schema: dump. Then look at schema.rb

schema.rb


 create_table "comments", force: :cascade do |t|
   (abridgement) 
  end

I had comments ... Delete unnecessary tables.

comments table deletion

Create a migration file and write the following

def change
 drop_table :comments
end

Goodbye with rails db: migrate. Now you can delete the review for both items! !!

Summary

I wrote it for a long time with just this, but the lesson I learned in this case is that machines will not betray, and it is the first person to doubt. The assumption is not good. It's my first post, so there are a lot of things I'm not sure about, but I hope it helps someone.

Recommended Posts

I was addicted to the record of the associated model
I was addicted to the setting of laradock + VSCode + xdebug
I was addicted to the roll method
I was addicted to the Spring-Batch test
I was addicted to the API version min23 setting of registerTorchCallback
Recorded because I was addicted to the standard input of the Scanner class
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was addicted to the NoSuchMethodError in Cloud Endpoints
I was addicted to starting sbt
What I was addicted to when introducing the JNI library
I was addicted to looping the Update statement on MyBatis
What I was addicted to with the Redmine REST API
The story I was addicted to when setting up STS
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
I was addicted to rewriting to @SpringApplicationConfiguration-> @SpringBootTest
About the matter that I was addicted to how to use hashmap
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
A memo that was soberly addicted to the request of multipart / form-data
Memorandum: What I was addicted to when I hit the accounting freee API
[Rails] I was addicted to the nginx settings when using Action Cable.
A story I was addicted to when testing the API using MockMVC
I was addicted to a simple test of Jedis (Java-> Redis library)
Problems I was addicted to when building the digdag environment with docker
I was addicted to scrollview because I couldn't tap the variable size UIView
I was addicted to unit testing with the buffer operator in RxJava
I was addicted to using RXTX on Sierra
I want to output the day of the week
I want to var_dump the contents of the intent
I was addicted to installing Ruby/Tk on MacOS
I was addicted to doing onActivityResult () with DialogFragment
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
I was addicted to not being able to connect to AWS-S3 from the Docker container
I was a little addicted to the S3 Checksum comparison, so I made a note.
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I want to know the answer of the rock-paper-scissors app
[Rails] I don't know how to use the model ...
I want to display the name of the poster of the comment
GetXxxx of ResultSet was addicted to primitive type (Java)
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
I tried to summarize the basics of kotlin and java
What I was addicted to while using rspec on rails
I was addicted to setting default_url_options with Rails devise introduction
I want to expand the clickable part of the link_to method
I want to make a specific model of ActiveRecord ReadOnly
I want to change the log output settings of UtilLoggingJdbcLogger
[Swift] I tried to implement the function of the vending machine
A story I was addicted to in Rails validation settings
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment of WSL2 + Docker + VSCode
I was addicted to using Java's Stream API in Scala
I want to narrow down the display of docker ps
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to fetch another association of the parent model from the intermediate table with has_many
A story I was addicted to with implicit type conversion of ActiveRecord during unit testing