Rails 3的迁移:添加引用列?Rails

2023-09-09 21:58:25 作者:沉浮与世间

如果我创建一个新的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