There is a very similar question on SO. The answer given is as follows:
Delete user 11's comments first, or change the foreign key so it's "on
delete cascade" mode - deletion of the parent will cause automatic
deletion of the child records
But I thought that was the purpose of
dependent: :destroy. When you delete the record, it deletes its associations.
I have the following:
class Venue < ApplicationRecord
has_many :entrees, dependent: :destroy
class Entree < ApplicationRecord
I attempt the following:
DELETE FROM "venues"
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "venues" violates foreign key constraint "fk_rails_8fda228aa3" on table "entrees"
Am I missing the purpose of
dependent: :destroy? Also if
dependent: :destroy is only intended to delete the associations if they do not have foreign key constraints, then how can I delete the dependents with foreign key constraints? Is there a Rails CASCADE option?