Ruby-on-rails 在 Rails 4 中添加引用列迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22815009/
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
Add a reference column migration in Rails 4
提问by Don P
A user has many uploads. I want to add a column to the uploadstable that references the user. What should the migration look like?
一个用户有很多上传。我想在uploads表中添加一列引用user. 迁移应该是什么样的?
Here is what I have. I'm not sure if I should use (1) :user_id, :intor (2) :user, :references. I'm not even sure if (2) works. Just trying to do this the "rails" way.
这是我所拥有的。我不确定我是否应该使用 (1):user_id, :int或 (2) :user, :references。我什至不确定(2)是否有效。只是试图以“轨道”的方式做到这一点。
class AddUserToUploads < ActiveRecord::Migration
def change
add_column :uploads, :user_id, :integer
end
end
Relevant question except for Rails 3. Rails 3 migrations: Adding reference column?
Rails 3以外的相关问题。Rails 3 迁移:添加参考列?
回答by Kirti Thorat
Rails 4.x
导轨 4.x
When you already haveusersand uploadstables and wish to add a new relationshipbetween them.
当您已经拥有users和uploads表并希望在它们之间添加新关系时。
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
Then, run the migration using rake db:migrate.
This migration will take care of adding a new column named user_idto uploadstable (referencing idcolumn in userstable), PLUS it will also add an index on the new column.
然后,使用rake db:migrate. 这种迁移会照顾添加一个名为新列user_id于uploads表(引用id列users表),再加上它也将在新列中添加索引。
UPDATE [For Rails 4.2]
更新 [对于 Rails 4.2]
Rails can't be trusted to maintain referential integrity; relational databasescome to our rescue here. What that means is that we can add foreign key constraints at the database levelitself and ensure that database would reject any operation that violates this set referential integrity. As @infoget commented, Rails 4.2ships 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.
不能信任 Rails 来维护引用完整性;关系数据库在这里帮助我们。这意味着我们可以在数据库级别本身添加外键约束,并确保数据库拒绝任何违反此集合参照完整性的操作。正如@infoget 评论的那样,Rails 4.2附带对外键(参照完整性)的本机支持。这不是必需的,但您可能希望将外键(因为它非常有用)添加到我们上面创建的引用中。
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
To create a completely brand new reference with a foreign key(in Rails 4.2), generate a migration using the following command:
要使用外键创建全新的引用(在 Rails 4.2 中),请使用以下命令生成迁移:
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
This will add a new foreign key to the user_idcolumn of the uploadstable. The key references the idcolumn in userstable.
这将向表的user_id列添加一个新的外键uploads。键引用表中的id列users。
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, mysql2and PostgreSQLadapters are supported. Don't try this with other adapters like sqlite3, etc. Refer to Rails Guides: Foreign Keysfor your reference.
注意:这是添加引用的补充,因此您仍然需要先创建引用,然后创建外键(您可以选择在同一个迁移或单独的迁移文件中创建外键)。Active Record 仅支持单列外键,目前仅支持mysql,mysql2和PostgreSQL适配器。不要在其他适配器(如sqlite3等)上尝试此操作。请参阅Rails 指南:外键以供您参考。
回答by Mirror318
Rails 5
导轨 5
You can still use this command to create the migration:
您仍然可以使用此命令来创建迁移:
rails g migration AddUserToUploads user:references
The migration looks a bit different to before, but still works:
迁移看起来与以前有点不同,但仍然有效:
class AddUserToUploads < ActiveRecord::Migration[5.0]
def change
add_reference :uploads, :user, foreign_key: true
end
end
Note that it's :user, not :user_id
请注意,它是:user,不是:user_id
回答by Kiry Meas
if you like another alternate approach with upand downmethod try this:
如果你喜欢另一种替代方法up和down方法,试试这个:
def up
change_table :uploads do |t|
t.references :user, index: true
end
end
def down
change_table :uploads do |t|
t.remove_references :user, index: true
end
end
回答by vantony
[Using Rails 5]
[使用 Rails 5]
Generate migration:
生成迁移:
rails generate migration add_user_reference_to_uploads user:references
This will create the migration file:
这将创建迁移文件:
class AddUserReferenceToUploads < ActiveRecord::Migration[5.1]
def change
add_reference :uploads, :user, foreign_key: true
end
end
Now if you observe the schema file, you will see that the uploads table contains a new field. Something like: t.bigint "user_id"or t.integer "user_id".
现在,如果您观察架构文件,您将看到上传表包含一个新字段。类似的东西:t.bigint "user_id"或t.integer "user_id"。
Migrate database:
迁移数据库:
rails db:migrate
回答by Nadeem Yasin
Another syntax of doing the same thing is:
做同样事情的另一种语法是:
rails g migration AddUserToUpload user:belongs_to
回答by Bruno Casali
Just to document if someone has the same problem...
只是为了记录是否有人有同样的问题......
In my situation I've been using :uuidfields, and the above answers does not work to my case, because rails 5 are creating a column using :bigintinstead :uuid:
在我的情况下,我一直在使用:uuid字段,而上述答案对我的情况不起作用,因为 rails 5 正在创建一个列,:bigint而不是使用:uuid:
add_column :uploads, :user_id, :uuid
add_index :uploads, :user_id
add_foreign_key :uploads, :users
回答by Clint Clinton
Create a migration file
创建迁移文件
rails generate migration add_references_to_uploads user:references
Default foreign key name
默认外键名称
This would create a user_id column in uploads table as a foreign key
这将在上传表中创建一个 user_id 列作为外键
class AddReferencesToUploads < ActiveRecord::Migration[5.2]
def change
add_reference :uploads, :user, foreign_key: true
end
end
user model:
用户模型:
class User < ApplicationRecord
has_many :uploads
end
upload model:
上传模型:
class Upload < ApplicationRecord
belongs_to :user
end
Customize foreign key name:
自定义外键名称:
add_reference :uploads, :author, references: :user, foreign_key: true
This would create an author_id column in the uploads tables as the foreign key.
这将在上传表中创建一个 author_id 列作为外键。
user model:
用户模型:
class User < ApplicationRecord
has_many :uploads, foreign_key: 'author_id'
end
upload model:
上传模型:
class Upload < ApplicationRecord
belongs_to :user
end

