在“引用”中指定列名称。移民 [英] Specifying column name in a "references" migration

查看:69
本文介绍了在“引用”中指定列名称。移民的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Rails中进行迁移,引用另一个表。通常,我会做以下事情:

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

add_column :post, :user, :references

这会在帖子 user_id 的列c $ c>表。但是,如果我要使用 author_id 这样的东西,而不是 user_id ,该怎么办?我该怎么办?

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

推荐答案

手动执行:

add_column :post, :author_id, :integer

但现在,在创建belongs_to语句时,必须对其进行修改,所以现在您必须调用

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

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

这篇关于在“引用”中指定列名称。移民的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆