【发布时间】:2021-02-11 16:29:41
【问题描述】:
如何编写迁移文件以向现有模型关系添加选项?必须保留现有的表数据。
例如,我已有:
class Chapter < ApplicationRecord
belongs_to :org
end
我要更新的内容:
class Chapter < ApplicationRecord
belongs_to :org, touch: true
end
如何为此编写迁移文件? (或者对于任何其他参考选项的更改?)
add_reference 会更新现有列吗?还是添加一个新的?
class AddChapterToOrg < ActiveRecord::Migration
def change
add_reference :org, :chapter, touch: true
end
end
【问题讨论】:
-
touch true 不需要任何数据库更改。没有迁移。您还有其他选择吗?像唯一性验证(尽管应该在 db 中具有唯一索引)或可选/必需(在 db 中应该有空检查)
标签: ruby ruby-on-rails-6