Ruby-on-rails Rails 3 迁移:添加参考列?

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

Rails 3 migrations: Adding reference column?

ruby-on-railsactiverecordmigration

提问by PlankTon

If I create a new rails 3 migration with (for example)

如果我创建一个新的 rails 3 迁移(例如)

rails g migration tester title:tester user:references

, everything works fine...however if I add a column with something along the lines of:

,一切正常……但是,如果我添加一列包含以下内容的列:

rails g migration add_user_to_tester user:references

the reference field is not recognised. In short, the question is: how do I add a referencing column to a rails migration from the command line?

无法识别参考字段。简而言之,问题是:如何从命令行向 rails 迁移添加引用列?

回答by Paulo Fidalgo

If you are using the Rails 4.xyou can now generate migrations with references, like this:

如果您使用的是Rails 4.x,您现在可以使用引用生成迁移,如下所示:

rails generate migration AddUserRefToProducts user:references

like you can see on rails guides

就像你在导轨上看到的那样

回答by DanneManne

EDIT: This is an outdated answer and should not be applied for Rails 4.x+

编辑:这是一个过时的答案,不应应用于Rails 4.x+

You don't need to add references when you can use an integer id to your referenced class.

当您可以对引用的类使用整数 id 时,您不需要添加引用。

I'd say the advantage of using references instead of a plain integer is that the model will be predefined with belongs_to and since the model is already created and will not be affected when you migrate something existing, the purpose is kind of lost.

我会说使用引用而不是普通整数的优点是模型将使用belongs_to 进行预定义,并且由于模型已经创建并且在您迁移现有内容时不会受到影响,因此目的有点丢失。

So I would do like this instead:

所以我会这样做:

rails g migration add_user_id_to_tester user_id:integer

And then manually add belongs_to :user in the Tester model

然后在Tester模型中手动添加belongs_to :user

回答by Eugene

Please note that you will most likely need an index on that column too.

请注意,您很可能也需要该列的索引。

class AddUserReferenceToTester < ActiveRecord::Migration
  def change
    add_column :testers, :user_id, :integer
    add_index  :testers, :user_id
  end
end

回答by Martin Cabrera Diaubalick

With the two previous steps stated above, you're still missing the foreign key constraint. This should work:

通过上述前两个步骤,您仍然缺少外键约束。这应该有效:

  class AddUserReferenceToTester < ActiveRecord::Migration
      def change
          add_column :testers, :user_id, :integer, references: :users
      end
  end

回答by gl03

You canuse references in a change migration. This is valid Rails 3.2.13 code:

可以在更改迁移中使用引用。这是有效的 Rails 3.2.13 代码:

class AddUserToTester < ActiveRecord::Migration
  def change
    change_table :testers do |t|
      t.references :user, index: true 
    end
  end
  def down
    change_table :testers do |t|
      t.remove :user_id
    end
  end
end

c.f.: http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table

参见:http: //apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table

回答by Wirwing

Running rails g migration AddUserRefToSponsors user:referenceswill generate the following migration:

运行rails g migration AddUserRefToSponsors user:references将生成以下迁移:

def change
  add_reference :sponsors, :user, index: true
end

回答by masterweily

That will do the trick:

这将做到这一点:

rails g migration add_user_to_tester user_id:integer:index

回答by Zamith

When adding a column you need to make that column an integer and if possible stick with rails conventions. So for your case I am assuming you already have a Tester and User models, and testers and users tables.

添加列时,您需要将该列设为整数,并且如果可能,请坚持使用 rails 约定。因此,对于您的情况,我假设您已经有一个 Tester 和 User 模型,以及 testers 和 users 表。

To add the foreign key you need to create an integer column with the name user_id (convention):

要添加外键,您需要创建一个名为 user_id 的整数列(约定):

add_column :tester, :user_id, :integer

Then add a belongs_to to the tester model:

然后添加一个belongs_to到tester模型:

class Tester < ActiveRecord::Base
  belongs_to :user
end

And you might also want to add an index for the foreign key (this is something the references already does for you):

而且您可能还想为外键添加一个索引(这是引用已经为您做的事情):

add_index :tester, :user_id

回答by Neha

You can add references to your model through command line in the following manner:

您可以通过以下方式通过命令行添加对模型的引用:

rails g migration add_column_to_tester user_id:integer

This will generate a migration file like :

这将生成一个迁移文件,如:

class AddColumnToTesters < ActiveRecord::Migration
  def change
    add_column :testers, :user_id, :integer
  end
end

This works fine every time i use it..

每次我使用它时都可以正常工作..

回答by shilovk

For Rails 4

对于 Rails 4

The generator accepts column type as references (also available as belongs_to).

生成器接受列类型作为引用(也可用作belongs_to)。

This migration will create a user_idcolumn and appropriate index:

此迁移将创建一个user_id列和适当的索引:

$ rails g migration AddUserRefToProducts user:references 

generates:

产生:

class AddUserRefToProducts < ActiveRecord::Migration
  def change
    add_reference :products, :user, index: true
  end
end

http://guides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration

http://guides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration

For Rails 3

对于 Rails 3

Helper is called references (also available as belongs_to).

Helper 称为引用(也可用作belongs_to)。

This migration will create a category_idcolumn of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the _idfor you.

此迁移将创建一个category_id适当类型的列。请注意,您传递的是模型名称,而不是列名称。Active Record_id为您添加了。

change_table :products do |t|
  t.references :category
end

If you have polymorphic belongs_toassociations then references will add both of the columns required:

如果您有多态belongs_to关联,则引用将添加所需的两列:

change_table :products do |t|
  t.references :attachment, :polymorphic => {:default => 'Photo'}
end

Will add an attachment_id column and a string attachment_typecolumn with a default value of Photo.

将添加一个attachment_id 列和attachment_type一个默认值为 的字符串列Photo

http://guides.rubyonrails.org/v3.2.21/migrations.html#creating-a-standalone-migration

http://guides.rubyonrails.org/v3.2.21/migrations.html#creating-a-standalone-migration