문제

In Datamapper, how would one specify the the combination of two fields must be unique. For example categories must have unique names within a domain:

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

  belongs_to :domain
end
도움이 되었습니까?

해결책

Did you try to define both properties as keys? Not sure I have tried it but that way they should become a composite key.

property :name, String, :key => true    
property :category, Integer, :key => true

다른 팁

You have to create a unique index for the two properties:

class Category
  include DataMapper::Resource

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

  belongs_to :domain
end

Actually, John, Joschi's answer is correct: the use of named :unique_index values does create a multiple-column index; it's important to read the right-hand side of those hash-rockets (i.e., if it had just been true, you would be right).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top