在 Rails 4 中添加引用列迁移 [英] Add a reference column migration in Rails 4
问题描述
一个用户有很多上传.我想向引用 user
的 uploads
表添加一列.迁移应该是什么样的?
A user has many uploads. I want to add a column to the uploads
table that references the user
. What should the migration look like?
这是我所拥有的.我不确定我是否应该使用 (1) :user_id, :int
或 (2) :user, :references
.我什至不确定(2)是否有效.只是想以轨道"的方式做到这一点.
Here is what I have. I'm not sure if I should use (1) :user_id, :int
or (2) :user, :references
. I'm not even sure if (2) works. Just trying to do this the "rails" way.
class AddUserToUploads < ActiveRecord::Migration
def change
add_column :uploads, :user_id, :integer
end
end
Rails 3 以外的相关问题.Rails 3 迁移:添加参考列?一个>
Relevant question except for Rails 3. Rails 3 migrations: Adding reference column?
推荐答案
Rails 4.x
当您已经users
和上传
表并希望在它们之间添加新关系时.
When you already have users
and uploads
tables and wish to add a new relationship between them.
您需要做的就是:只需使用以下命令生成迁移:
All you need to do is: just generate a migration using the following command:
rails g migration AddUserToUploads user:references
这将创建一个迁移文件:
Which will create a migration file as:
class AddUserToUploads < ActiveRecord::Migration
def change
add_reference :uploads, :user, index: true
end
end
然后,使用 rake db:migrate
运行迁移.此迁移将负责将名为 user_id
的新列添加到 uploads
表(引用 users
表中的 id
列),此外它还会在新列上添加索引.
Then, run the migration using rake db:migrate
.
This migration will take care of adding a new column named user_id
to uploads
table (referencing id
column in users
table), PLUS it will also add an index on the new column.
更新 [对于 Rails 4.2]
无法信任 Rails 来维护引用完整性;关系数据库在这里帮助我们.这意味着我们可以在数据库级别本身添加外键约束,并确保数据库拒绝任何违反此集合参照完整性的操作.正如@infoget 评论的那样,Rails 4.2 附带对外键(参照完整性) 的本机支持.这不是必需的,但您可能希望将外键(因为它非常有用)添加到我们上面创建的引用中.
Rails can’t be trusted to maintain referential integrity; relational databases come to our rescue here. What that means is that we can add foreign key constraints at the database level itself and ensure that database would reject any operation that violates this set referential integrity. As @infoget commented, Rails 4.2 ships with native support for foreign keys(referential integrity). It's not required but you might want to add foreign key(as it's very useful) to the reference that we created above.
要将外键添加到现有引用,请创建新迁移以添加外键:
To add foreign key to an existing reference, create a new migration to add a foreign key:
class AddForeignKeyToUploads < ActiveRecord::Migration
def change
add_foreign_key :uploads, :users
end
end
要使用外键创建一个全新的引用(在 Rails 4.2 中),请使用以下命令生成迁移:
To create a completely brand new reference with a foreign key(in Rails 4.2), generate a migration using the following command:
rails g migration AddUserToUploads user:references
这将创建一个迁移文件:
which will create a migration file as:
class AddUserToUploads < ActiveRecord::Migration
def change
add_reference :uploads, :user, index: true
add_foreign_key :uploads, :users
end
end
这将向 uploads
表的 user_id
列添加一个新的外键.键引用 users
表中的 id
列.
This will add a new foreign key to the user_id
column of the uploads
table. The key references the id
column in users
table.
注意:这是除了添加引用之外,因此您仍然需要先创建引用,然后创建外键(您可以选择创建外键键在同一个迁移或单独的迁移文件中).Active Record 仅支持单列外键,目前仅支持 mysql
、mysql2
和 PostgreSQL
适配器.不要在 sqlite3
等其他适配器上尝试此操作.请参阅 Rails Guides: Foreign键供您参考.
NOTE: This is in addition to adding a reference so you still need to create a reference first then foreign key (you can choose to create a foreign key in the same migration or a separate migration file). Active Record only supports single column foreign keys and currently only mysql
, mysql2
and PostgreSQL
adapters are supported. Don't try this with other adapters like sqlite3
, etc. Refer to Rails Guides: Foreign Keys for your reference.
这篇关于在 Rails 4 中添加引用列迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!