gpt4 book ai didi

ruby-on-rails - rails : why cannot delete dependent table if dependent: :destroy allowed?

转载 作者:行者123 更新时间:2023-12-01 14:31:01 28 4
gpt4 key购买 nike

article_id 必须在 Pictures 表中,因为每篇文章可以有许多图片。

所以图片表看起来像这样:

id        article_id         picture

1 34 pinguin.jpg
2 56 koala.jpg
3 56 bear.jpg

等等

我知道destroy_all(它在依赖表上实例化一个对象并调用它的destroy方法,这是一个缓慢的过程)和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

架构
 create_table "articles", force: :cascade do |t|
t.string "country"
t.string "region"
t.string "town"
t.string "street"
t.string "company"
t.string "title"
t.text "content"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "articles", ["user_id", "created_at"], name: "index_articles_on_user_id_and_created_at", using: :btree
add_index "articles", ["user_id"], name: "index_articles_on_user_id", using: :btree

create_table "pictures", force: :cascade do |t|
t.integer "article_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "picture"
end

所以,这两个命令,Article.delete_all 和 Article.destroy_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

更新

我曾尝试在迁移文件中添加 on delete 级联 FK,但无济于事:

我首先删除了 FK,因为我必须添加限定符:on_delete::cascade
class RemoveFkFromPictures < ActiveRecord::Migration
def change

remove_foreign_key :pictures, column: :article_id
end
end

我创建了另一个迁移文件来添加 FK
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

最佳答案

首先,我要感谢您的洞察力和乐于助人的良好意愿。

从你离开的地方开始,我告诉你我是如何解决这个问题的,我花了两天时间,直到今天醒来,我想我可以尝试一下。

1) 事实上,在 Parent 模型上只写依赖::destroy 是不够的。当您实际上是指“删除,级联”时,Ruby 似乎实际上并没有完成“不留下孤儿”的工作,被认为是用“销毁”来表示的。

2)当您生成创建 Fk 的模型时,例如 user:references,它确实创建了一个 FK,您可以在 schema.rb 文件中看到它,但是,正如您所指出的,默认情况下是“限制”,即执行对 child 没什么。

3)然后我们就完蛋了。因为Herodes 和FK 都不会破坏。

4)您需要从迁移文件中删除 FK,将其替换为带有“on delete cascade”的 FK



5)您需要删除仍然位于模型中的依赖::销毁。

6) 这样,低级数据库命令将接管并且删除进行干净和快速。

关于ruby-on-rails - rails : why cannot delete dependent table if dependent: :destroy allowed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35814659/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com