postgresql Rails 的 ActiveRecord::Migration 的外键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2572949/
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-20 00:05:40  来源:igfitidea点击:

Foreign keys with Rails' ActiveRecord::Migration?

ruby-on-railspostgresqlforeign-keysmigration

提问by Earlz

I'm new to Ruby on Rails (I know Ruby just decently though) and looking at the Migration tools, it sounds really awesome. Database schemas can finally (easily) go in source control.

我是 Ruby on Rails 的新手(虽然我很了解 Ruby)并且查看迁移工具,这听起来真的很棒。数据库模式最终可以(轻松)进入源代码控制。

Now my problem with it. When using Postgres as the database, it does not setup foreign keys. I would like the benefits of foreign keys in my schema such as referential integrity. So how do I apply foreign keys with Migrations?

现在我的问题。当使用 Postgres 作为数据库时,它不会设置外键。我想在我的架构中使用外键的好处,例如参照完整性。那么如何在迁移中应用外键呢?

采纳答案by mikewilliamson

Rails philosophy is that integrity checks is business logic that belongs in the model. Thats why you are seeing what you are seeing in the DB; whatever_id is just an int and not a "real" fk in sight. Its not a mistake, its by design and its a little freaky at first. Generally the only reason that drives people to work with fks in the DB level is when the DB is accessed by more than one app or its a legacy system. There is plenty of discussion and some great links here: Why do Rails migrations define foreign keys in the application but not in the database?

Rails 的理念是完整性检查是属于模型的业务逻辑。这就是为什么您会看到您在数据库中看到的内容;whatever_id 只是一个 int 而不是一个“真正的”fk。这不是一个错误,它的设计和一开始有点怪异。通常,促使人们在 DB 级别使用 fks 的唯一原因是 DB 被多个应用程序或其遗留系统访问时。这里有很多讨论和一些很好的链接:为什么 Rails 迁移在应用程序中而不是在数据库中定义外键?

回答by glebm

Check this out: http://github.com/matthuhiggins/foreigner

看看这个:http: //github.com/matthuhiggins/foreigner

But first make sure you really need them (e.g. referential integrity is something that theoretically shouldn't break as long as your code is OK, and you know about :dependent => :destroyand the difference between user.deleteand user.destroy).

但首先要确保你确实需要它们(如引用完整性的东西,理论上不应该,只要你的代码是OK分手,你知道:dependent => :destroy之间的差异user.deleteuser.destroy)。

回答by simianarmy

There are a number of plugins available (search google) for Rails that will create foreign keys for you when you use a special symbol in your migrations (foreign_key_migrations is one from the Advanced Rails Recipes book). Just be aware that Rails does not play well with this concept especially when you are trying to delete objects (as mentioned by glebm).

Rails 有许多可用的插件(搜索谷歌),当您在迁移中使用特殊符号时,它们会为您创建外键(foreign_key_migrations 是 Advanced Rails Recipes 书中的一个)。请注意,Rails 不能很好地处理这个概念,尤其是当您尝试删除对象时(如 glebm 所述)。

回答by Extrapolator

I just came across this post. Maybe someone will find it useful. That's how the constraints are created:

我刚看到这个帖子。也许有人会发现它很有用。这就是约束的创建方式:

http://guides.rubyonrails.org/migrations.html#using-reversible

http://guides.rubyonrails.org/migrations.html#using-reversible

class ExampleMigration < ActiveRecord::Migration
def change
create_table :products do |t|
  t.references :category
end

reversible do |dir|
  dir.up do
    #add a foreign key
    execute <<-SQL
      ALTER TABLE products
        ADD CONSTRAINT fk_products_categories
        FOREIGN KEY (category_id)
        REFERENCES categories(id)
    SQL
  end
  dir.down do
    execute <<-SQL
      ALTER TABLE products
        DROP FOREIGN KEY fk_products_categories
    SQL
  end
end

add_column :users, :home_page_url, :string
rename_column :users, :email, :email_address
end