Ruby on rails 删除记录违反外键约束

Ruby on rails 删除记录违反外键约束,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个用于通知的表,其中存储了来自多个模型的ID,因此用户在与在您的文章上发表评论的人相关的操作时会收到通知。这些通知只属于它将要访问的用户,而不属于任何东西,不过,它只是引用了事物的ID。因此,现在每当我尝试删除一篇文章时,例如,我都会收到一个错误: ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "comments" violates foreign ke

我有一个用于通知的表,其中存储了来自多个模型的ID,因此用户在与在您的文章上发表评论的人相关的操作时会收到通知。这些通知只属于它将要访问的用户,而不属于任何东西,不过,它只是引用了事物的ID。因此,现在每当我尝试删除一篇文章时,例如,我都会收到一个错误:

ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:  update or delete on table "comments" violates foreign key constraint "fk_rails_9268535f02" on table "notifications"
如何将其设置为同时删除任何引用的通知

通知的My db架构:

create_table "notifications", force: :cascade do |t|
t.integer  "recipient_id"
t.integer  "notified_by_id"
t.integer  "glip_id"
t.integer  "article_id"
t.integer  "group_id"
t.integer  "post_id"
t.integer  "comment_id"
t.integer  "notation_id"
t.integer  "response_id"
t.integer  "remark_id"
t.integer  "conversation_id"
t.integer  "message_id"
t.boolean  "read",              default: false
t.datetime "created_at",                        null: false
t.datetime "updated_at",                        null: false
t.integer  "notification_type"
t.index ["article_id"], name: "index_notifications_on_article_id"
t.index ["comment_id"], name: "index_notifications_on_comment_id"
t.index ["conversation_id"], name: "index_notifications_on_conversation_id"
t.index ["glip_id"], name: "index_notifications_on_glip_id"
t.index ["group_id"], name: "index_notifications_on_group_id"
t.index ["message_id"], name: "index_notifications_on_message_id"
t.index ["notation_id"], name: "index_notifications_on_notation_id"
t.index ["notified_by_id"], name: "index_notifications_on_notified_by_id"
t.index ["post_id"], name: "index_notifications_on_post_id"
t.index ["recipient_id"], name: "index_notifications_on_recipient_id"
t.index ["remark_id"], name: "index_notifications_on_remark_id"
t.index ["response_id"], name: "index_notifications_on_response_id"
结束

和我的通知模型:

class Notification < ApplicationRecord
  enum notification_type: { comment: 0, notation: 1, message: 2, feature: 3, marked_helpful: 4,
                            participation: 5, response: 6, remark: 7, follow: 8, unfollow: 9 }
  enum read: { read: true, unread: false }
  belongs_to :notified_by, class_name: 'User'
end
您的用户应使用:

类用户<应用程序记录 ... 有多个:通知、外键::由\u id通知、依赖::销毁 终止 如果在关联的“属于”侧使用:class\u名称,则使用:foreign\u键

现在,当您删除用户时,所有引用用户的通知也将被删除

您可以对文章或引用的任何其他模型重复相同的过程。

您的用户应使用:

类用户<应用程序记录 ... 有多个:通知、外键::由\u id通知、依赖::销毁 终止 如果在关联的“属于”侧使用:class\u名称,则使用:foreign\u键

现在,当您删除用户时,所有引用用户的通知也将被删除

您可以对文章或引用的任何其他模型重复相同的过程。

这可能是因为在删除文章时,关联的注释一起被删除,并且在db级别的注释和通知表之间存在外键约束,从而阻止删除

我的建议是在注释模型中添加dependent::destroy,如果您希望关联的通知与注释一起被删除

class Comment < ApplicationRecord
  has_many :notifications, dependent: destroy
end
如果您希望在删除注释时不删除通知

class Comment < ApplicationRecord
  has_many :notifications, dependent: :nullify
end
有关更多详细信息,请查看

之间的差异可能是由于删除文章时,关联的注释将一起删除,并且在db级别的注释和通知表之间存在外键约束,从而阻止删除

我的建议是在注释模型中添加dependent::destroy,如果您希望关联的通知与注释一起被删除

class Comment < ApplicationRecord
  has_many :notifications, dependent: destroy
end
如果您希望在删除注释时不删除通知

class Comment < ApplicationRecord
  has_many :notifications, dependent: :nullify
end
有关更多详细信息,请查看

进入并使依赖者破坏的一切都有很多的不同之处在于这个技巧。进入并使依赖者破坏的一切都有很多的不同之处在于这个技巧。我不知道这个依赖者::使它无效。谢谢。@ddonche-Hmm给出的答案比你接受的答案更具解释性。这可能会让未来的搜索者感到困惑:希望这是有意义的。我不知道这个dependent::nullify的事情。谢谢。@ddonche-Hmm给出的答案比你接受的答案更具解释性。这可能会让未来的搜索者感到困惑:希望这是有意义的。