Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

ValidatesUniqueness mismatches the implementation of unique indexes in dm-constraints #43

Open
tillsc opened this issue Sep 21, 2011 · 1 comment
Milestone

Comments

@tillsc
Copy link
Contributor

tillsc commented Sep 21, 2011

Given a model

class Foo
  property :num1, Integer, :unique => :scope_name
  property :num2, Integer, :unique => :scope_name
  belongs_to :bar, :unique => :scope_name
end

This example results into a perfectly working unique index "unique_foos_scope_name" with three fields ('num1', 'num2' and 'bar_id'). But the validation won't work and would throw the following exception:

raise(ArgumentError,"Could not find property to scope by: #{subject}. Note that :unique does not currently support arbitrarily named groups, for that you should use :unique_index with an explicit validates_uniqueness_of.")

Following the instructions from the exception this would lead to the following model:

class Foo
  property :num1, Integer
  property :num2, Integer, :unique => [:num1, :bar]
  belongs_to :bar
end

Now the Validation is working but the generated indexes are crap (unique index with only one field: 'num2').

The only solution to fix this would be to address the constraints separately:

class Foo
  property :num1, Integer, :unique_index => :index_name
  property :num2, Integer, :unique => [:num1, :bar], :unique_index => :index_name
  belongs_to :bar, :unique_index => :index_name
end

But this also breaks the application because belongs_to can't handle the :unique_index option correctly. So you'll have to use:

class Foo
  property :num1, Integer, :unique_index => :index_name
  property :num2, Integer, :unique => [:num1, :bar], :unique_index => :index_name
  belongs_to :bar
  property :bar_id, :unique_index => :index_name, :index => true
end

This seems to work but I don't think this is what the most people want :-)

@tillsc
Copy link
Contributor Author

tillsc commented Sep 21, 2011

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant