我有一个非常简单的表格如何设置唯一约束超过200列时,一列可以为空
categories(parent_id, title)
我想设置一个唯一的约束,使两类不能有相同的标题和父母。
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.integer :parent_id
t.string :title, :null => false
end
add_index :categories, [:title, :parent_id], :unique => true
end
end
当PARENT_ID为null,不会强制标题这是我们所需要的独特性。是否有可能确保标题对于根类别是唯一的?