#1015 new
PreciousBodilyFluids

Erratic behavior when using dm-is-list with primary keys

Reported by PreciousBodilyFluids | August 20th, 2009 @ 04:51 PM

Hi,

I'm trying to use edge DataMapper with an edge Rails project where I'm using the position attribute as a primary key on some of my models. I got some strange behavior, and I managed to emulate it in the rb file that's attached (and copy-pasted below). I commented everything, so it should be pretty self-evident.

I dug through is-list to try to figure out what's going wrong, but I didn't get far. I'll try again, but I wanted to post this in the meantime.

Thanks!


require "rubygems"
require "dm-core"
require "dm-is-list"

# Setup DataMapper. I'm using Postgres, but I don't think it matters.

class Question
  include DataMapper::Resource

  property :position, Integer, :key => true

  is :list

  has n, :answers, :child_key => [:question_position]
end

class Answer
  include DataMapper::Resource

  property :question_position, Integer, :key => true
  property :position, Integer, :key => true

  is :list, :scope => [:question_position]

  belongs_to :question, :child_key => [:question_position]
end

Question.auto_migrate!
Answer.auto_migrate!




# Creating a child through an association returns the wrong object.

q1 = Question.create
a1 = q1.answers.create
a2 = q1.answers.create
a3 = q1.answers.create

puts q1.inspect   #=> #<Question @position=1>
puts a1.inspect   #=> #<Answer @question_position=1 @position=1>
puts a2.inspect   #=> #<Answer @question_position=1 @position=2>
puts a3.inspect   #=> #<Answer @question_position=1 @position=2>

puts a1.eql? a2   #=> false
puts a2.eql? a3   #=> true

try_again = Answer.get(1,3)
puts try_again.inspect   #=> #<Answer @question_position=1 @position=3>



# Can't move objects around list.

begin
  try_again.move(:highest)   #=> ERROR:  duplicate key value violates unique constraint "answers_pkey" (DataObjects::IntegrityError)
  rescue DataObjects::IntegrityError
end



# Finally, if the Answer class has any other properties, there's a duplicate key problem on the fourth object made.

q2 = Question.create
20.times do
  q2.answers.create
end   #=> No problem

class Answer
  property :title, String
end
Answer.auto_migrate!

q3 = Question.create
20.times do
  q3.answers.create
end   #=>   ERROR:  duplicate key value violates unique constraint "answers_pkey" (DataObjects::IntegrityError)

Comments and changes to this ticket

  • PreciousBodilyFluids

    PreciousBodilyFluids August 24th, 2009 @ 02:15 PM

    Hi -

    I'm still new enough to ruby that the datamapper internals are kind of beyond me, but I thought I'd at least write a spec to cover the use case of having the position attribute be part of the key. I'm not sure how to begin, though. The issue of the object not being movable in the list feels like a retread of the tests for a uniquely indexed position, which isn't supported right now either. The other two issues (with creating new objects) seem like they might be more of a dm-core issue. So I'm not sure what I should be speccing.

    Suggestions?

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 »

People watching this ticket

Attachments

Pages