1

I'm using DataMapper connected to an SQLite backend. I need to create a Unique index across my four belongs_to columns. Here is the table.

class Score 
include DataMapper::Resource

property :id, Serial
property :score, Integer

belongs_to :pageant
belongs_to :candidate
belongs_to :category
belongs_to :judge

#What we want is a  UNIQUE INDEX on the four properties!

end

Things I've done:

  1. A unique index on the four via something like :unique_index => :single_score. This works only if you have a property already included.
  2. validates_uniqueness_of, I think the scope only works for a 2-column unique index.
  3. My current solution, which is to just create a dummy field "dont_mind_me", just so I can put :unique_index => single_score in it and everything works. Is this something that's okay to do?
  4. Create an index using raw SQL, SQLite supports a unique index among the four fields.

Basically there are two parts of this question:

  1. Is my solution okay, or should I find another one? I'm at wit's end dealing with what seems to be something trivial, even with raw SQL
  2. How do I create an "after_create_table" hook in DataMapper? The hooks in the documentation only tell about post-CRUD data.
Daryll Santos
  • 2,031
  • 3
  • 22
  • 40

0 Answers0