I am currently facing a problem with a legacy database for which I am
creating a SQLAlchemy layer. With regards to mirroring the columns
everything goes well. However I am having difficulties defining a
relationship which is not many-to-many, but does use something which can
be compared with an association proxy (I think). The simplified model:


class Session(Base):
    __tablename__ = 'sessions'
    id = Column(Integer, primary_key=True)


class Area(Base):
    __tablename__ = 'areas'
    session_id = Column(Integer, ForeignKey('sessions.id'),
    primary_key=True)
    area_id = Column(Integer, primary_key=True)
    name = Column(String)


class Parent(Base):
    __tablename__ = 'parents'
    id = Column(Integer, primary_key=True)
    session_id = Column(Integer, ForeignKey('sessions.id'))


class Child(Base):
    __tablename__ = 'children'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer,  ForeignKey('parents.id'))
    area_id = Column(Integer)

    area = relationship(???)
    

In short; the Area class has a multi-column primary key (sesseion_id,
area_id). I am trying to create the relationship for area in the Child
class, however this would need the session_id which can be found in the
parent class. This was my last effort (again assuming I could treat the
parent class as a association proxy):

    area = relationship(
        'Area',
        secondary='parents',
        primaryjoin='children.c.parent_id==parents.c.id',
        secondaryjoin='and_(parents.c.session_id==areas.c.session_id,
        children.c.area_id==areas.c.id)',
        viewonly=True
    )

No able to figure out any foreign key relations if those would be
required.

Any help and/or insight would be appreciated,

Thijs

-- 
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.

Reply via email to