Confirm uniqueness in combo fields in DataMapper

I want to be able to enter entries in my database, where the manufacturer will be presented several times, but does not match the combination of the manufacturer and model. So “Sony (manufacturer), TV (model)” is okay “Sony (manufacturer), OtherTv (model)”, but the third entry “Sony (manufacturer), TV (model)” is not ok, because the combination of manufacturer and model not unique. I tried with validation :key => true, but it doesn't seem to work. And I can’t do something like validates_uniqueness_of :manufacturer AND :modelI think. So how do you do this?

class Tvs
  include DataMapper::Resource

  property :id,           Serial
  property :manufacturer, String, :key => true
  property :model,        String, :key => true

  validates_uniqueness_of :
end
+3
source share
4

:

class Tvs
  include DataMapper::Resource

  property :id,           Serial
  property :manufacturer, String, :unique_index => :manufacturer_model
  property :model,        String, :unique_index => :manufacturer_model

  validates_uniqueness_of :model, :scope => :manufacturer
end

.

+5

Nevermind. , :

class Tvs
  include DataMapper::Resource

  property :id,           Serial
  property :manufacturer, String, :unique_index => true
  property :model,        String, :unique_index => true

  validates_uniqueness_of :model, :scope => :manufacturer
end
0
class Tvs
  include DataMapper::Resource

  property :id,           Serial
  property :manufacturer, String, :unique_index => :manufacturer_model
  property :model,        String, :unique_index => :manufacturer_model

  validates_is_unique :model, :scope => :manufacturer
end

I needed to modify the example using "validates_is_unique" to make this work.

0
source

If you delete the line property :id Serial, you will get a composite key (Manufacturer, model), which will not allow duplicating models for a specific manufacturer.

0
source

Source: https://habr.com/ru/post/1771913/


All Articles