我想使用自定义类型列将 STI 添加到现有表中。我们称这个taste_type 对应的模型是Fruit。
在水果模型中,我有:
set_inheritance_column :taste_type
在我添加 STI 的迁移中,我有:
class AddSTI < ActiveRecord::Migration
def self.up
add_column :fruits, :taste_type, :string, :limit => 100, :null => false
Fruit.reset_column_information
Fruit.find_by_id(1).update_attributes({:taste_type => 'Sour'})
end
def self.down
remove_column :fruits, :taste_type
end
end
当我运行迁移时,我收到以下错误:
Mysql::Error: Column 'taste_type' cannot be null: ...
知道发生了什么吗?如果我在 Fruit 模型中注释 set_inheritance_column,我可以运行迁移,然后在运行迁移后取消注释。但是,显然,我不想这样做。