Home ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table violates foreign key constraint
Reply: 1

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table violates foreign key constraint

Donato
1#
Donato Published in 2018-01-12 21:30:38Z

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?

fool-dev
2#
fool-dev Reply to 2018-01-13 04:07:01Z

Try to the following

Venue.destroy_all

It's working for me

Venue.delete_all

Not working

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.349126 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO