#1347 ✓not-applicable
John E. Vincent

primary key via association only

Reported by John E. Vincent | June 30th, 2010 @ 12:44 PM

See the following gist:


If I remove the property definitions in the Skill model, I'm getting the following error running DataMapper.auto_migrate!:

DataMapper::IncompleteModelError: Skill must have a key to be valid

I don't mind having to define them other than the extra typing but should that be required? I'm obviously intending to create a compound PK.

Comments and changes to this ticket

  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) July 5th, 2010 @ 08:37 AM

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


    You need to explicitly add the :key => true option to both your belongs_to declarations in the Skill model, if you want them to form a CPK. By default, FK properties established by calls to belongs_to are not considered to be keys.

    That said, you might hit another issue when leaving out the explicit property definitions. DM doesn't (yet) respect the order in which the relationships were defined (at least not on 1.8.x where Hashes aren't ordered by default). This means that the oder in which your PK properties are defined when automigrating, is not stable. This will lead to problems when you want to call get() but you don't know the proper order in which the key components must be given to get() (which is the order in which they were defined).

    I'll mark this ticket as non-applicable but I will open up a new one for the other issue I described here.

  • John E. Vincent

    John E. Vincent July 5th, 2010 @ 08:44 AM


    I'll take another look but even with the :key => true to each belongs_to, that's how I got the original error (Skill must have key). I'll rerun it again and see if that was indeed the case.

  • John E. Vincent

    John E. Vincent July 5th, 2010 @ 08:53 AM

    Okay I've verified it's working properly with the key statements moved down and the explicit defines removed.

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 »