views:

31

answers:

1

I found that this issue had been discussed in Ticket #58 of DataMapper, apparently way back in 2007, but I can't find how to do it in the latest version (dm-core-0.10.2). I want to define two composite indexes, each of which are partially based on a certain property. I was hoping I could do this...

class Stat
  include DataMapper::Resource
  property :id,            Serial,
  property :collected_on,  Integer #yyyyMMddhhmm
  property :measure,       Integer
  property :dimension_one, Integer
  property :dimension_two, Integer
  property :source_id,     Integer
  index [:collected_on, :dimension_one, :dimension_two]
  index [:source_id, :collected_on]
end

What is the correct way to do it?

+2  A: 

You can do this:

class Stat
  include DataMapper::Resource
  property :id,            Serial,
  property :collected_on,  Integer, :index => [ :index_one, :index_two ]
  property :measure,       Integer
  property :dimension_one, Integer, :index => :index_one
  property :dimension_two, Integer, :index => :index_one
  property :source_id,     Integer, :index => :index_two
end

Of course you can make the indexes anything you like. The indexes can be an Array, or a Symbol as shown above, or even just true if you want to put the property in an index by itself and you don't care what the index is called.

dkubb
Thanks! I guess this could work in the specific example I gave as long as I moved the :source_id property above the :collected_on property, so that :index_two will be [:source_id,:collected_on] instead of [:collected_on,:source_id]... but what would you do if you need an index on both orders?
Chris