在 Datamapper 中,如何指定两个字段的组合必须是唯一的。例如,类别在域中必须具有唯一名称:
class Category
include DataMapper.resource
property :name, String, :index=>true #must be unique for a given domain
belongs_to :domain
end
在 Datamapper 中,如何指定两个字段的组合必须是唯一的。例如,类别在域中必须具有唯一名称:
class Category
include DataMapper.resource
property :name, String, :index=>true #must be unique for a given domain
belongs_to :domain
end
您必须为这两个属性创建一个唯一索引:
class Category
include DataMapper::Resource
property :name, String, :unique_index => :u
property :domain_id, Integer, :unique_index => :u
belongs_to :domain
end
实际上,John Joschi 的回答是正确的:使用命名的 :unique_index 值确实会创建多列索引;阅读这些散列火箭的右侧很重要(即,如果它只是true
,那么您是对的)。
您是否尝试将这两个属性都定义为键?不确定我是否尝试过,但这样它们应该成为复合键。
property :name, String, :key => true
property :category, Integer, :key => true