On Wed, Sep 23, 2009 at 5:34 PM, Conor <conor.edward.da...@gmail.com> wrote:

>
> You are missing a foreign key column in the "people" table that
> corresponds to your Person->Company relation. As a result, SQLAlchemy
> tries to use person.id as the foreign key column (because that column
> happens to be a foreign key to a base table of Company) and everything
> blows up.
>

Doh!  I knew I was missing something basic.

So:
> * Add a foreign key column to Person that refers to companies.id.
> * Add a primaryjoin argument to your Persion.company relation, because
> SQLAlchemy will now see two potential ways to get from Person to
> Company (people.id -> bizentities.bizentity_id and people.company_id -
> > companies.id) and refuse to guess which path to take. Try
> "primaryjoin=lambda: Person.company_id == Company.__table__.c.id". I'm
> using Company.__table__.c.id instead of Company.id because Company.id
> maps to the "bizentities.bizentity_id" column and not the
> "companies.id" column.
>
> -Conor
>
>
Ah.  I wouldn't have thought of the primaryjoin bit, so it's a good thing
you mentioned it. The traceback you get if you leave it out is a little
intimidating. :)

Thanks for this, Connor!  Everything works now.
For the benefit of future searchers, here was the final model:

class BizEntity(Base):
    __tablename__ = 'biz_entities'
    id = Column('bizentity_id', Integer, primary_key=True)
    type =  Column('bizentity_type', String(30), nullable=False)
    __mapper_args__ = {'polymorphic_on': type}

class Company(BizEntity):
    __tablename__ = 'companies'
    id = Column(Integer, ForeignKey('biz_entities.bizentity_id'),
primary_key=True)
    name = Column('company_name', String(50))
    __mapper_args__ = {'polymorphic_identity': 'company'}

    def __init__(self, company_name):
        self.name = company_name

    def __repr__(self):
        return "<Company('%s')>" % (self.name)


class Person(BizEntity):
    __tablename__ = 'people'
    id = Column('bizentity_id', Integer,
ForeignKey('biz_entities.bizentity_id'), primary_key=True)
    first_name = Column('first_name', String(50))
    middle_init = Column('middle_init', String(1))
    last_name = Column('last_name', String(50))

    company_id = Column(Integer, ForeignKey('companies.id'))
    company = relation(Company,
                        primaryjoin=lambda: Person.company_id ==
Company.__table__.c.id,
                        backref=backref('employees', order_by=id))

    __mapper_args__ = {'polymorphic_identity':'person'}

    def __init__(self, first_name, middle_init, last_name):
        self.first_name = first_name
        self.middle_init = middle_init
        self.last_name = last_name

    def __repr__(self):
        return "<Person('%s %s. %s')>" % (self.first_name, self.middle_init,
self.last_name)


Kevin Horn

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to