在Rails 4中添加引用列迁移

本文翻译自:Add a reference column migration in Rails 4

A user has many uploads. 用户有很多上传。 I want to add a column to the uploads table that references the user . 我想在uploads表中添加一个引用该user What should the migration look like? 迁移应该是什么样的?

Here is what I have. 这就是我所拥有的。 I'm not sure if I should use (1) :user_id, :int or (2) :user, :references . 我不确定是否应该使用(1) :user_id, :int或(2) :user, :references I'm not even sure if (2) works. 我甚至不确定(2)是否有效。 Just trying to do this the "rails" way. 只是试图以“轨道”的方式做到这一点。

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迁移:添加引用列?


#1楼

参考:https://stackoom.com/question/1xJe1/在Rails-中添加引用列迁移


#2楼

Rails 4.x Rails 4.x

When you already have users and uploads tables and wish to add a new relationship between them. 当您已经拥有 usersuploads表格并希望在它们之间添加新关系时

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 . 然后,使用rake db:migrate运行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. 此迁移将负责添加名为user_id的新列到uploads表(引用users表中的id列),PLUS它还将在新列上添加索引。

UPDATE [For Rails 4.2] 更新[For Rails 4.2]

Rails can't be trusted to maintain referential integrity; 不能信任Rails来维护引用完整性; 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. 这意味着我们可以在数据库级别自己添加外键约束,并确保数据库拒绝任何违反此set参照完整性的操作。 As @infoget commented, Rails 4.2 ships with native support for foreign keys(referential integrity) . 正如@infoget所评论的那样, Rails 4.2附带了对外键的本机支持(参照完整性) 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

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_id column of the uploads table. 这将向uploads表的user_id列添加新的外键。 The key references the id column in users table. 该键引用users表中的id列。

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. Active Record仅支持单列外键,目前仅支持mysqlmysql2PostgreSQL适配器。 Don't try this with other adapters like sqlite3 , etc. Refer to Rails Guides: Foreign Keys for your reference. 不要尝试使用sqlite3等其他适配器。请参阅Rails指南:外键供您参考。


#3楼

Rails 5 Rails 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 ,not :user_id


#4楼

做同样事情的另一种语法是:

rails g migration AddUserToUpload user:belongs_to

#5楼

[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. 现在,如果您观察架构文件,您将看到uploads表包含一个新字段。 Something like: t.bigint "user_id" or t.integer "user_id" . 类似于: t.bigint "user_id"t.integer "user_id"

Migrate database: 迁移数据库:

rails db:migrate

#6楼

if you like another alternate approach with up and down method try this: 如果你喜欢另一种替代方法有updown的方法试试这个:

  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值