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

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

Description

@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:

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 :-)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions