Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Open
tillsc opened this Issue · 1 comment

1 participant

@tillsc

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:
https://github.com/datamapper/dm-validations/blob/c874faff876fdabe00f224031921e28cdebced5f/lib/dm-validations/validators/uniqueness_validator.rb#L39

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 tillsc referenced this issue in datamapper/dm-core
Open

Bug on create unique index with scopes #281

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.