Rails 3的迁移:添加引用列? [英] Rails 3 migrations: Adding reference column?

查看:142
本文介绍了Rails 3的迁移:添加引用列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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?

推荐答案

您并不需要添加引用时,你可以使用一个整数ID来你引用的类。

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

我会说使用的,而不是一个普通的整数引用的优点是,该模型将pdefined与belongs_to的并且由于已经创建的模型,当你迁移现有的东西也不会受到影响$ P $,目的是那种失去了。

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.

所以,我会做而不是像这样:

So I would do like this instead:

rails g migration add_user_id_to_tester user_id:integer

然后手动添加belongs_to的:用户在测试模型

And then manually add belongs_to :user in the Tester model

这篇关于Rails 3的迁移:添加引用列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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