对应的回滚是什么?
我有以下 ActiveRecord 迁移:
class CreateSubjects < ActiveRecord::Migration
def self.up
create_table :subjects do |t|
t.string :title
t.timestamps
end
change_table :projects do |t|
t.references :subjects
end
end
def self.down
drop_table :subjects
remove_column :projects, :subjects_id #defeats the purpose of having references
end
end
我实际上喜欢 references
样式。不幸的是,我在 self.down
部分中找不到与 references
等效的回滚。如果我写 remove_column :projects, :subjects_id
我也可以写 t.integer :subjects_id
,这样会更安全。
I have the following ActiveRecord migration:
class CreateSubjects < ActiveRecord::Migration
def self.up
create_table :subjects do |t|
t.string :title
t.timestamps
end
change_table :projects do |t|
t.references :subjects
end
end
def self.down
drop_table :subjects
remove_column :projects, :subjects_id #defeats the purpose of having references
end
end
I actually like the references
style. Unfortunately I could not find the rollback equivalent of references
in the self.down
section. If I write remove_column :projects, :subjects_id
I can as well write t.integer :subjects_id
, which would make it safer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它称为remove_references。
当心! Rails 按照惯例使用单数,应该是:
It is called remove_references.
Be careful! Rails uses singular by convention, should be: