Ruby on rails Rails:如果允许dependent::destroy,为什么不能删除依赖表?
文章id必须在图片表中,因为每篇文章可以有许多图片 因此,图片表如下所示:Ruby on rails Rails:如果允许dependent::destroy,为什么不能删除依赖表?,ruby-on-rails,Ruby On Rails,文章id必须在图片表中,因为每篇文章可以有许多图片 因此,图片表如下所示: id article_id picture 1 34 pinguin.jpg 2 56 koala.jpg 3 56 bear.jpg 等 我知道destroy_all(它实例化依赖表上的一个对象并调用其destroy方法,这是一个缓慢的过程)和del
id article_id picture
1 34 pinguin.jpg
2 56 koala.jpg
3 56 bear.jpg
等
我知道destroy_all(它实例化依赖表上的一个对象并调用其destroy方法,这是一个缓慢的过程)和delete_all之间的区别,delete_all只清除记录而不考虑关联
但我必须使用destroy_all,因为存在相关的从属记录:
Article Model
class Article < ActiveRecord::Base
belongs_to :user
has_many :pictures , **`dependent: :destroy`**
accepts_nested_attributes_for :pictures
Picture Model
class Picture < ActiveRecord::Base
belongs_to :article
mount_uploader :picture, PictureUploader
def destroy
end
因此,这两个命令,Article.delete\u all和Article.destroy\u all
提出类似投诉
Article.destroy_all
Article Load (0.9ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."created_at" DESC
(0.1ms) BEGIN
SQL (1.7ms) DELETE FROM "articles" WHERE "articles"."id" = $1 [["id", 21]]
PG::ForeignKeyViolation: ERROR: update or delete on table "articles" violates foreign key constraint "fk_rails_658164416e" on table "pictures"
DETAIL: Key (id)=(21) is still referenced from table "pictures".
: DELETE FROM "articles" WHERE "articles"."id" = $1
(0.2ms) ROLLBACK
class CreatePictures < ActiveRecord::Migration
def change
create_table :pictures do |t|
t.references :article, index: true, foreign_key: true
t.timestamps null: false
end
end
end
所以,你会认为现在一切都好了。但事实并非如此:
Article Load (1.3ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."created_at" DESC
(0.2ms) BEGIN
Picture Load (0.5ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."article_id" = $1 [["article_id", 25]]
(0.4ms) ROLLBACK
ActiveRecord::RecordNotDestroyed: Failed to destroy the record
首先,我要感谢你的洞察力和善意的帮助 从你离开的地方开始,我告诉你我是如何解决这个问题的,我用了两天的时间,直到今天醒来,我想我可以试试 1) 实际上,仅在父模型中编写dependent::destroy是不够的。看起来Ruby实际上并没有做“不留下孤儿”的工作,而实际上你的意思是“删除,级联”,被认为是“销毁” 2) 当您生成一个创建Fk的模型时,比如user:references,它确实创建了一个可以在schema.rb文件中看到的Fk,但是,正如您所指出的,默认情况下是“restrict”,即不对子对象执行任何操作 3) 那我们就完蛋了。因为既不会毁灭希律王,FK也不会 4) 您需要从迁移文件中删除FK,将其替换为“on delete cascade” 及 5) 您需要删除仍然位于模型中的dependent::destroy
6) 这样,低级数据库命令将接管,删除过程将干净、快速地进行。那些**不在您的模型中,是吗,您只是用它们来突出显示。您可以创建在rails中级联删除的fk。这一切都取决于您是希望rails删除它还是数据库。您需要自己编写迁移代码。生成器无法为您执行此操作。请使用删除\u外键,然后在te迁移中添加\u外键来更改它。此外,您还可以使用execute运行自己的SQL,直到确认为止。这并没有解决问题。我重置了整个数据库,删除了迁移文件。删除外键,使用架构文件检查外键是否首先存在(没有on-delete级联),然后创建另一个迁移添加on-delete级联,如下所示:add\u-foreign\u-key:pictures,:articles,on-delete::cascade架构文件清楚地显示了更改并显示了以下信息:add\u-foreign\u-key“文章”,“用户”添加\u外键“图片”,“文章”,在\u delete::cascade end上,然后我尝试删除,我得到错误:无法销毁记录。
class RemoveFkFromPictures < ActiveRecord::Migration
def change
remove_foreign_key :pictures, column: :article_id
end
end
class AddForeignKeyToPictures1 < ActiveRecord::Migration
def change
add_foreign_key :pictures, :articles, on_delete: :cascade , name: :deletingarticlescascadesonpictures
end
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_foreign_key "articles", "users"
add_foreign_key "pictures", "articles", name: "deletingarticlescascadesonpictures", on_delete: :cascade
end
Article Load (1.3ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."created_at" DESC
(0.2ms) BEGIN
Picture Load (0.5ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."article_id" = $1 [["article_id", 25]]
(0.4ms) ROLLBACK
ActiveRecord::RecordNotDestroyed: Failed to destroy the record