Nis shres
Nis shres

Reputation: 49

PG::ForeignKeyViolation: ERROR: update or delete on table

PG::ForeignKeyViolation: ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_c98ef61810" on table "licences"
DETAIL:  Key (id)=(7) is still referenced from table "licences".

I already have belongs_to :approved_by_admin, class_name: 'Admin', optional: true

But STill gives me this error why ?

    add_reference :licences, :approved_by_admin, foreign_key: { to_table: :users }

Upvotes: 1

Views: 1471

Answers (1)

Ursus
Ursus

Reputation: 30056

Ok, the user want to set to null the reference in licenses when a user is deleted, but does not want to touch the schema. I think a dependent: :nullify on the has_many association for admins (application level) might be the solution

# admin
has_many :dont_know_the_association, dependent: :nullify

Upvotes: 4

Related Questions