尝试删除具有从属记录的记录时抛出ActiveRecord :: InvalidForeignKey错误

时间:2017-10-30 17:22:38

标签: ruby-on-rails

我有课程UploadOverrideOverride belongs_to :upload,以及每个Upload has_one :override。当我尝试在实际上与destroy关联的任何Upload上调用Override时,我会在控制台中收到此消息的致命回复:

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

我已经实现了外键迁移方法la SUPER USER's question以及来自Ricky Mason's questiondependent: destroy方法(例如,Upload包含has_one :override, dependent: :destroy }),但我仍然得到ForeignKeyViolation错误。

如何删除特定的上传记录及其相关的覆盖记录?

编辑:

每个型号的代码:

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


class Override < ApplicationRecord
  belongs_to :upload
end

请原谅我,如果我误解了你需要什么样的架构,你可以找到我的schema results here

要重现错误,Override对象必须与Upload对象关联。之后,您需要在.destroy对象上调用Upload

0 个答案:

没有答案