Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Composite primary key created with a 'property' and 'belongs_to' declaration requires property to be unique #254

Open
jdarais opened this issue Jun 12, 2013 · 1 comment

Comments

@jdarais
Copy link

jdarais commented Jun 12, 2013

It seems DataMapper doesn't behave properly when a composite key is split between a foreign key declared with 'belongs_to' and a second, non-unique column declared with 'property'.

Example:

class Person
    include DataMapper::Resource

    property :id,   Integer,    :key => true
    has n, :aliases
end

class Alias
    include DataMapper::Resource

    property :alias_num,    Integer,    :key => true
    property :alias,        String
    belongs_to :person, :key => true
end

personA = Person.new(:id => 1)
personA.aliases.new(:alias_num => 1, :alias => 'Geoffery')
personA.save

personB = Person.new(:id => 2)
personB.aliases.new(:alias_num => 1, :alias => 'Richard')
personB.save

results in error: "column alias_num is not unique"

When 'alias_num' is not required to be unique because it is part of a composite key. Currently, the erroneous behavior can be circumvented by also adding a property declaration to match the belongs_to declaration:

class Alias
    include DataMapper::Resource

    property :alias_num,    Integer,    :key => true
    property :alias,        String
    property :person_id,     Integer,    :key => true
    belongs_to :person, :key => true
end

But it seems like such an extra declaration shouldn't be necessary

@HQ063
Copy link

HQ063 commented Oct 13, 2013

instead of adding the association_id property, you can add :unique_index=>false to the column declared with property

class Alias
    include DataMapper::Resource

    property :alias_num,    Integer,    :key => true, :unique_index => false
    property :alias,        String
    belongs_to :person, :key => true
end

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants