Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datamapper multi-field unique index

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
like image 887
John F. Miller Avatar asked Sep 02 '09 22:09

John F. Miller


1 Answers

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
like image 155
joschi Avatar answered Oct 29 '22 23:10

joschi