Ruby-on-rails 在“引用”迁移中指定列名

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

Specifying column name in a "references" migration

ruby-on-railsruby-on-rails-3activerecordrails-migrations

提问by caarlos0

I want to make a migrationin Rails, referencing another table. Usually, I would do something like:

我想migration在 Rails 中创建一个,引用另一个表。通常,我会这样做:

add_column :post, :user, :references

This creates a column named user_idin poststable. But what if, instead of user_id, I want something like author_id? How can I do that?

这将创建一个名为列user_idposts表中。但是如果,而不是user_id,我想要类似的东西author_id呢?我怎样才能做到这一点?

采纳答案by mschultz

Do it manually:

手动执行:

add_column :post, :author_id, :integer

but now, when you create the belongs_to statement, you will have to modify it, so now you have to call

但是现在,当你创建belongs_to 语句时,你将不得不修改它,所以现在你必须调用

def post
    belongs_to :user, :foreign_key => 'author_id'
end

回答by ecoologic

In Rails 4.2+you can also set foreign keysin the db as well, which is a great idea.

Rails 4.2+ 中,您还可以在 db 中设置外键这是一个好主意

For simple associations this can be done also on t.referencesadding foreign_key: true, but in this case you'll need two lines.

对于简单的关联,这也可以通过t.references添加来完成foreign_key: true,但在这种情况下,您需要两行。

# The migration
add_reference :posts, :author, index: true
add_foreign_key :posts, :users, column: :author_id

# The model
belongs_to :author, class_name: "User"

回答by Sheharyar

For Rails 5+

对于 Rails 5+

Initial Definition:

初始定义:

If you are defining your Postmodel table, you can set references, indexand foreign_keyin one line:

如果您正在定义Post模型表,则可以在一行中设置references,indexforeign_key

t.references :author, index: true, foreign_key: { to_table: :users }


Update Existing:

更新现有:

If you are adding references to an existing table, you can do this:

如果要添加对现有表的引用,可以执行以下操作:

add_reference :posts, :author, foreign_key: { to_table: :users }


Note:The default value for indexis true.

注意:默认值为indextrue。

回答by nathanvda

In rails 4, when using postgresql and the schema_plusgem you can just write

在 rails 4 中,当使用 postgresql 和schema_plusgem 时,你可以只写

add_reference :posts, :author, references: :users

This will create a column author_id, which correctly refers to users(id).

这将创建一个列author_id,它正确地引用users(id).

And in your model, you write

在你的模型中,你写

belongs_to :author, class_name: "User"

Note, when creating a new table you can write it as follows:

请注意,在创建新表时,您可以按如下方式编写:

create_table :things do |t| 
  t.belongs_to :author, references: :users 
end 

Note: the schema_plusgem in it's entirety is not compatible with rails 5+, but this functionality is offered by the gem schema_auto_foreign_keys(part of schema_plus) which is compatible with rails 5.

注意:整个schema_plusgem 与 rails 5+ 不兼容,但此功能由与 rails 5 兼容的 gem schema_auto_foreign_keys(schema_plus 的一部分)提供。

回答by jes5199

If you aren't using a foreign key, then it doesn't matter what the actual table name of the other table is.

如果您没有使用外键,那么另一个表的实际表名是什么并不重要。

add_reference :posts, :author

As of Rails 5, if you're using a foreign key, you can specify the name of the other table in the foreign key options. (see https://github.com/rails/rails/issues/21563for discussion)

从 Rails 5 开始,如果您使用外键,则可以在外键选项中指定另一个表的名称。(讨论见https://github.com/rails/rails/issues/21563

add_reference :posts, :author, foreign_key: {to_table: :users}

Prior to Rails 5, you should add the foreign key as a separate step:

在 Rails 5 之前,您应该添加外键作为单独的步骤:

add_foreign_key :posts, :users, column: :author_id

回答by sekmo

alias_attribute(new_name, old_name)is very handy. Just create your model and the relationship:

alias_attribute(new_name, old_name)非常方便。只需创建您的模型和关系:

rails g model Post title user:references

then edit the model and add an attribute alias with

然后编辑模型并添加属性别名

alias_attribute :author, :user

After that you'll be able to run things like

之后,您将能够运行诸如

Post.new(title: 'My beautiful story', author: User.first)