#444 ✓resolved
massk

#belongs_to foreign key property not created immediately

Reported by massk | July 6th, 2008 @ 08:02 AM

The foreign key property is not created when calling belongs_to until the association is accessed on an instance of the class.

class Person
  include DataMapper::Resource
  property :id, Serial
end

class Pet
  include DataMapper::Resource
  property :id, Serial
  belongs_to :person
end

Pet.new.person_id #=> NoMethodError
Pet.count(:person_id => 1) #=> ArgumentError: Clause :person_id does not map to a DataMapper::Property
Pet.new(:person_id => 1) #=> NameError: person_id= is not a public property

all of the above works after you call Pet#person

Pet.new.person #=> nil

Pet.new.person_id #=> nil
Pet.count(:person_id => 1) #=> 0
Pet.new(:person_id => 1) #=> #<Pet id=nil person_id=1>

this spec should have caught this, but #slice returns [nil] so the spec always passes, even though it should fail.

I have attached a patch fixing the spec above (so that it fails)

and a new spec that shows the behaviour I would have expected.

Comments and changes to this ticket

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

Referenced by

Pages