0

I have classes Upload and Override. Override belongs_to :upload, and each Upload has_one :override. When I try to call destroy on any Upload that does in fact have an Override associated with it, I get a fatal response in the console with this message:

ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:  update or delete on table "uploads" violates foreign key constraint "fk_rails_ae7fe83ba8" on table "overrides"

I have implemented both the foreign key migration approach a la SUPER USER's question, as well as the dependent: destroy method from Ricky Mason's question (as in, Upload contains has_one :override, dependent: :destroy), and yet I still get the ForeignKeyViolation error.

How do I go about deleting a particular Upload record along with its dependent Override record?

EDIT:

Code for each model:

class Upload < ApplicationRecord
  has_one :override, dependent: :destroy
end


class Override < ApplicationRecord
  belongs_to :upload
end

Forgive me if I misunderstood what you needed as far as schema goes, but you can find my schema results here.

To reproduce the error, an Override object has to be associated with an Upload object. Afterwards, you need to call .destroy on that Upload object.

0 Answers0