带有Rails ActiveRecord的外键::迁移?

时间:2022-09-20 19:02:05

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作为数据库时,它不设置外键。我希望在我的模式中引入外键的好处,比如引用完整性。那么如何将外键应用到迁移中呢?

4 个解决方案

#1


13  

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的哲学是完整性检查是属于模型的业务逻辑。这就是为什么你在DB上看到的东西;whatever_id只是一个int类型,而不是“真正的”fk。它不是一个错误,它的设计和它有点怪。通常,促使人们在DB级别上使用fks的唯一原因是多个应用程序或其遗留系统访问DB。这里有大量的讨论和一些很棒的链接:为什么Rails迁移在应用程序中定义外键而在数据库中不定义外键?

#2


7  

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 => :destroy and the difference between user.delete and user.destroy).

但是首先要确保你确实需要它们(例如,引用完整性是理论上不应该中断的,只要你的代码是OK的,并且你知道:依赖=>:destroy和user.delete和user.destroy之间的区别)。

#3


1  

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是来自高级Rails菜谱)。请注意,Rails不能很好地使用这个概念,尤其是当您试图删除对象时(如glebm所述)。

#4


0  

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利用可逆

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

#1


13  

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的哲学是完整性检查是属于模型的业务逻辑。这就是为什么你在DB上看到的东西;whatever_id只是一个int类型,而不是“真正的”fk。它不是一个错误,它的设计和它有点怪。通常,促使人们在DB级别上使用fks的唯一原因是多个应用程序或其遗留系统访问DB。这里有大量的讨论和一些很棒的链接:为什么Rails迁移在应用程序中定义外键而在数据库中不定义外键?

#2


7  

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 => :destroy and the difference between user.delete and user.destroy).

但是首先要确保你确实需要它们(例如,引用完整性是理论上不应该中断的,只要你的代码是OK的,并且你知道:依赖=>:destroy和user.delete和user.destroy之间的区别)。

#3


1  

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是来自高级Rails菜谱)。请注意,Rails不能很好地使用这个概念,尤其是当您试图删除对象时(如glebm所述)。

#4


0  

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利用可逆

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