#1492 ✓not-applicable
Siegfried Levin

When violate the unique index, no error raised.

Reported by Siegfried Levin | March 1st, 2011 @ 11:14 PM

I've got two models in my application.

class Person
  include DataMapper::Resource

  # Primary Key
  property :id, Integer, :key => true

  # References
  has n, :loans, "Ticket", :child_key => [:loaner_id], :constraint => :protect
  has n, :debts, "Ticket", :child_key => [:loanee_id], :constraint => :protect
end

class Ticket
  include DataMapper::Resource

  # Primary Key
  property :id, Serial

  # General Properties
  property :loan_id, UUID, :required => true, :default => lambda { |r, p| UUIDTools::UUID.random_create }, :unique_index => :loan_index
  property :loaner_id, Integer, :required => true, :unique_index => :loan_index
  property :debt_id, UUID, :required => true, :default => lambda { |r, p| UUIDTools::UUID.random_create }, :unique_index => :debt_index
  property :loanee_id, Integer, :required => true, :unique_index => :debt_index
  property :amount, Decimal, :scale => 2, :required => true
  property :started_on, Date, :required => true
  property :expired_on, Date
  property :description, String, :length => 140, :lazy => true
  property :status, Enum[:pending, :active, :cancelled, :closed], :default => :pending, :required => true

  # References
  belongs_to :loaner, "Person"
  belongs_to :loanee, "Person"

  # Validations
  validates_with_block { loaner != loanee }
  validates_with_block(:if => :expired_on) { started_on <= expired_on }
end

I think when record violate the unique index, there should be a DataObjects::IntegrityError, but I find it go with another way. It change the field after save.

ruby-1.9.2-head :003 > Ticket.first
 => #<Ticket @id=1 @loan_id=#<UUID:0x81c06288 UUID:37496e44-498e-456f-bcf8-7360df77e680> @loaner_id=5871 @debt_id=#<UUID:0x81c05fcc UUID:03b4a7e2-fdc2-46f0-99e3-0e9c1d9be45e> @loanee_id=49341 @amount=#<BigDecimal:10380d2a8,'0.3623E5',9(18)> @started_on=#<Date: 2011-02-01 (4911187/2,0,2299161)> @expired_on=#<Date: 2011-03-21 (4911283/2,0,2299161)> @description=<not loaded> @status=:pending> 
ruby-1.9.2-head :004 > ticket = Ticket.make(Ticket.first.attributes.select { |k, v| [:loan_id, :loaner_id].include?(k) })
 => #<Ticket @id=nil @loan_id=#<UUID:0x81c0ba94 UUID:37496e44-498e-456f-bcf8-7360df77e680> @loaner_id=5871 @debt_id=nil @loanee_id=45753 @amount=#<BigDecimal:104a069f0,'0.77546E5',9(18)> @started_on=#<Date: 2011-02-20 (4911225/2,0,2299161)> @expired_on=#<Date: 2011-03-15 (4911271/2,0,2299161)> @description="Entailment poritidae circummeridian leptoprosope ma" @status=nil> 
ruby-1.9.2-head :005 > ticket.save
 => true 
ruby-1.9.2-head :006 > ticket
 => #<Ticket @id=11 @loan_id=#<UUID:0x81c0ba94 UUID:37496e44-498e-456f-bcf8-7360df77e680> @loaner_id=87862 @debt_id=#<UUID:0x81c15a44 UUID:6e16f5ee-bc51-4d3a-a3d6-e6219208b86b> @loanee_id=45753 @amount=#<BigDecimal:104a069f0,'0.77546E5',9(18)> @started_on=#<Date: 2011-02-20 (4911225/2,0,2299161)> @expired_on=#<Date: 2011-03-15 (4911271/2,0,2299161)> @description="Entailment poritidae circummeridian leptoprosope ma" @status=:pending>

Comments and changes to this ticket

  • Siegfried Levin

    Siegfried Levin March 2nd, 2011 @ 01:23 AM

    A validation does not work either.

    class Ticket
      include DataMapper::Resource
    
      # Primary Key
      property :id, Serial
    
      # General Properties
      property :loan_id, UUID, :required => true, :default => lambda { |r, p| UUIDTools::UUID.random_create }
      property :debt_id, UUID, :required => true, :default => lambda { |r, p| UUIDTools::UUID.random_create }
      property :amount, Decimal, :precision => 10, :scale => 2, :required => true
      property :started_on, Date, :required => true
      property :expired_on, Date
      property :description, String, :length => 140, :lazy => true
      property :status, Enum[:pending, :active, :cancelled, :closed], :default => :pending, :required => true
    
      # References
      belongs_to :loaner, "Person"
      belongs_to :loanee, "Person"
    
      # Validations
      validates_with_block { loaner != loanee }
      validates_with_block(:if => :expired_on) { started_on <= expired_on }
      validates_uniqueness_of :loan_id, :scope => :loaner_id
      validates_uniqueness_of :debt_id, :scope => :loanee_id
    end
    
    ruby-1.9.2-head :001 > Ticket.gen
     => #<Ticket @id=1 @loan_id=#<UUID:0x81edb6fc UUID:d0bebaca-1398-4d48-abbf-9f356e960c26> @debt_id=#<UUID:0x80cc23e4 UUID:2e2f2592-6bf3-4460-b33b-1ade41e79959> @amount=#<BigDecimal:100bc7770,'0.61123E5',9(18)> @started_on=#<Date: 2011-02-28 (4911241/2,0,2299161)> @expired_on=#<Date: 2011-03-31 (4911303/2,0,2299161)> @description="Pyrotechnics expulser orthopinacoidal heatlike corr" @status=:pending @loaner_id=69168 @loanee_id=83227> 
    ruby-1.9.2-head :002 > ticket = Ticket.make(Ticket.first.attributes.select { |k, v| [:loan_id, :loaner_id].include?(k) })
     => #<Ticket @id=nil @loan_id=#<UUID:0x805efe7c UUID:d0bebaca-1398-4d48-abbf-9f356e960c26> @debt_id=nil @amount=#<BigDecimal:103dca420,'0.93197E5',9(18)> @started_on=#<Date: 2011-02-14 (4911213/2,0,2299161)> @expired_on=#<Date: 2011-03-29 (4911299/2,0,2299161)> @description="Alfirk pseudoequalitarian retile endopericarditis a" @status=nil @loaner_id=69168 @loanee_id=59452> 
    ruby-1.9.2-head :003 > ticket.valid?
     => true 
    ruby-1.9.2-head :004 > ticket.save
     => true 
    ruby-1.9.2-head :005 > ticket
     => #<Ticket @id=2 @loan_id=#<UUID:0x805efe7c UUID:d0bebaca-1398-4d48-abbf-9f356e960c26> @debt_id=#<UUID:0x80cc98c4 UUID:a3dc6c8e-9bf2-47eb-a325-fdd364abf858> @amount=#<BigDecimal:103dca420,'0.93197E5',9(18)> @started_on=#<Date: 2011-02-14 (4911213/2,0,2299161)> @expired_on=#<Date: 2011-03-29 (4911299/2,0,2299161)> @description="Alfirk pseudoequalitarian retile endopericarditis a" @status=:pending @loaner_id=23091 @loanee_id=59452>
    
  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) March 5th, 2011 @ 09:49 AM

    • State changed from “new” to “not-applicable”

    Validation of a unique key works fine. I made an example which shows the correct setup of your models: https://gist.github.com/856457

    I checked with DM 1.1.0.rc2

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages