On May 17, 2011, at 4:50 AM, roderick wrote:
> Hi,
> I have a problem with relationships between objects which inherit the
> same parent:
>
> class Person(DeclarativeBase):
> __tablename__ = 'person'
> id = Column(Integer, primary_key=True)
> name = Column(Unicode(100), nullable=False
>
> def __init__(self, name):
> self.name = name
>
> class Employee(Person):
> __tablename__ = 'employee'
> id = Column(Integer, ForeignKey('person.id'), primary_key=True)
>
> def __init__(self, name):
> Person.__init__(self, name)
>
> class Customer(Person):
> __tablename__ = 'customer'
> id = Column(Integer, ForeignKey('person.id'), primary_key=True)
> employee_id = Column(Integer, ForeignKey('employee.id'))
> employee = relationship("Employee", backref="employees"
>
> def __init__(self, name, employee):
> Person.__init__(self, name)
> self.employee = employee
>
>
> The error:
>
> One or more mappers failed to initialize - can't proceed with
> initialization of other mappers. Original exception was: Could not
> determine join condition between parent/child tables on relationship
> Customer.employee. Specify a 'primaryjoin' expression. If 'secondary'
> is present, 'secondaryjoin' is needed as well.
>
> Without generalisation there is no problem.
When a joined-inheritance relationship references itself, there are multiple
ways that the two tables might link to each other, in that there are two
foreign keys between the two entities. Above the Customer may link to
Employee via Customer.id->Person.id, or via Customer.employee_id->Employee.id.
The message indicates that this condition should be specified explicitly
using "primaryjoin". An overview of "primaryjoin" is at:
http://www.sqlalchemy.org/docs/orm/relationships.html#specifying-alternate-join-conditions-to-relationship
.
In 0.7 you'd be able to say:
employee = relationship("Employee", backref="employees",
primaryjoin=employee_id==Employee.id)
However 0.6 has a glitch, in that Employee.id is actually going to prioritize
the "id" column on the "person" table, since it has the same name as
Employee.id, which is not what you want here. So in 0.6, to ensure the join
that you want is set up:
employee = relationship("Employee", backref="employees",
primaryjoin=employee_id==Employee.__table__.c.id)
>
> Thanks in advance!
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.