Ruby-on-rails 我可以在 Rails 中设置级联删除吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/331394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:48:39  来源:igfitidea点击:

Can I set up Cascade deleting in Rails?

ruby-on-railsrubydatabase

提问by matt_dev

I know this is probably on the Internet somewhere but I can't find the answer here on Stackoverflow so I thought I may boost up the knowledge base here a little.

我知道这可能是在互联网上的某个地方,但我在 Stackoverflow 上找不到答案,所以我想我可以在这里稍微增加一下知识库。

I'm a newbie to Ruby and Rails but my company is getting pretty invested in it so I'm trying to get to know it in a little more detail.

我是 Ruby 和 Rails 的新手,但我的公司在这方面投入了大量资金,所以我试图更详细地了解它。

It's been difficult for me to change my mindset to designing an application from the "model" rather the from the database, so I'm trying to figure out how would do all of the design work that I have classically done in the Database in the Rails model instead.

我很难改变我的思维方式,从“模型”而不是从数据库设计应用程序,所以我试图弄清楚如何完成我在数据库中经典完成的所有设计工作Rails 模型代替。

So the most recent task that I have given myself is to figure out how to configure a Rails database model to do cascading deletes? Is there an easy way of doing this? Or would I have to go into the MySql and set this up?

所以我最近给自己的任务是弄清楚如何配置 Rails 数据库模型来执行级联删除?有没有简单的方法来做到这一点?或者我是否必须进入 MySql 并进行设置?

回答by Mike Breen

you can also set the :dependent option to :delete_all. :delete_all will issue a single SQL statement to delete all child records. because of this using :delete_all may give you better performance.

您还可以将 :dependent 选项设置为 :delete_all。:delete_all 将发出一条 SQL 语句来删除所有子记录。因此,使用 :delete_all 可能会给您带来更好的性能。

has_many :memberships, dependent: :delete_all

回答by danmayer

Yeah you can, if you are using a relationship like has_many you just do this

是的,你可以,如果你使用像 has_many 这样的关系,你就这样做

has_many :memberships, dependent: :destroy

回答by Hendrik

Contrary to the provided answer I highly suggest also doing this on a database level. In case you have different processes or a multi threaded environment it could happen that records are not properly deleted. Furthermore the database foreign key makes things way faster when deleting lots of data.

与提供的答案相反,我强烈建议在数据库级别也这样做。如果您有不同的进程或多线程环境,可能会发生记录未正确删除的情况。此外,数据库外键在删除大量数据时使事情变得更快。

Like in the suggested answer do this:

就像在建议的答案中那样:

has_many :memberships, dependent: :delete_all

However also make sure to setup a foreign_keyin a migration. That way the database takes care of deleting the records automatically for you.

但是,还要确保foreign_key在迁移中设置 a 。这样,数据库会自动为您删除记录。

To nullify the values when a membership is deleted, assuming you have a user model:

要在删除成员资格时使值无效,假设您有一个用户模型:

add_foreign_key :users, :memberships, on_delete: :nullify

You can also delete all the models whenever a membership is deleted

您还可以在删除成员资格时删除所有模型

add_foreign_key :users, :memberships, on_delete: :cascade

回答by Jarin Udom

Just keep in mind that delete_all will not execute any callbacks (like before_destroy and after_destroy) on the child records.

请记住,delete_all 不会对子记录执行任何回调(如 before_destroy 和 after_destroy)。

回答by Sean McMains

It looks like this plugin might give you what you're looking for if you want the cascading deletes reflected in the actual database structure:

如果您希望级联删除反映在实际数据库结构中,看起来这个插件可能会为您提供您正在寻找的内容:

http://www.redhillonrails.org/foreign_key_migrations.html

http://www.redhillonrails.org/foreign_key_migrations.html

Format for using this in a migration would be something like this:

在迁移中使用它的格式将是这样的:

create_table :orders do |t|
  t.column :customer_id, :integer, :on_delete => :set_null, :on_update => :cascade
  ...
end