通过添加和删除引用进行 Rails 迁移
使用rails生成迁移AddClientToUser创建迁移文件后,我可以像这样编辑我的迁移文件:
class AddClientToUser < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.references :client
end
end
def self.down
change_table :users do |t|
t.remove :client_id
end
end
end
这是反转迁移中添加的参考列的正确方法吗?
After creating a migration file with rails generate migration AddClientToUser
I can edit my migration file like so:
class AddClientToUser < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.references :client
end
end
def self.down
change_table :users do |t|
t.remove :client_id
end
end
end
Is this the correct way to reverse the reference column added in the migration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Rails 4.2.1
将生成类似的迁移:
此外,可以通过
在
change
方法中添加: 来自由添加另一个或其他引用。最后在保存对迁移的更改后运行
rake db:migrate
,将产生所需的结果。Rails 4.2.1
Will generate a migration similar:
In addition, one is at liberty to add another or other reference(s) by adding:
within the very
change
method.And finally running
rake db:migrate
after saving the changes to the migration, will produce the desired results.没错!你也可以选择:
that is right! and you could also go with:
在rails 4之后,您可以执行以下操作
它将为您处理向上和向下,以及创建外键索引。您还可以使用
remove_reference
< /a> 做相反的事情。After rails 4 you can do the following
It will handle the up and the down for you, as well as creating a foreign key index. You can also use
remove_reference
to do the opposite.使用 Rails 4,您只需输入:
,这将与 Ryan 所说的相同。
With Rails 4 you can just type:
in the console and this will make the same that Ryan said.