8

在 Datamapper 中,如何指定两个字段的组合必须是唯一的。例如,类别在域中必须具有唯一名称:

class Category
  include DataMapper.resource
  property :name, String, :index=>true #must be unique for a given domain

  belongs_to :domain
end
4

3 回答 3

16

您必须为这两个属性创建一个唯一索引:

class Category
  include DataMapper::Resource

  property :name, String, :unique_index => :u
  property :domain_id, Integer, :unique_index => :u

  belongs_to :domain
end
于 2010-01-05T01:16:34.720 回答
2

实际上,John Joschi 的回答是正确的:使用命名的 :unique_index 值确实会创建多列索引;阅读这些散列火箭的右侧很重要(即,如果它只是true,那么您是对的)。

于 2012-04-13T00:10:02.817 回答
1

您是否尝试将这两个属性都定义为键?不确定我是否尝试过,但这样它们应该成为复合键。

property :name, String, :key => true    
property :category, Integer, :key => true
于 2009-09-02T22:53:51.303 回答