我有一個非常簡單的表格如何設置唯一約束超過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,不會強制標題這是我們所需要的獨特性。是否有可能確保標題對於根類別是唯一的?