Reputation: 9407
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
end
class Entree < ApplicationRecord
belongs_to :venue
end
I attempt the following:
Venue.delete_all
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?
Upvotes: 2
Views: 4289
Reputation: 165
Its Working try This
ActiveRecord::Base.connection.disable_referential_integrity do
Book.destroy_all
Genre.destroy_all
end
Upvotes: 4
Reputation: 7777
Try to the following
Venue.destroy_all
It's working for me
Venue.delete_all
Not working
Upvotes: 0