更新时间:2023-12-01 09:04:40
Rails迁移没有提供任何添加约束的方法,但是您仍然可以通过迁移来实现,但只需将实际的SQL传递给execute()
Rails migration does not provide any way to add Constraints, but you can still do it via migration but by passing actual SQL to execute()
创建迁移文件:
ruby script/generate Migration AddConstraint
现在,在迁移文件中:
class AddConstraint < ActiveRecord::Migration
def self.up
execute "ALTER TABLE table_name ADD CONSTRAINT check_constraint_name CHECK (check_column_name IN (1, 2, 3) )"
end
def self.down
execute "ALTER TABLE table_name DROP CONSTRAINT check_constraint_name"
end
end