For anyone with SQLAlchemy experience, this will be fundamental, I am sure; But I do not find documents that are useful to me, and I'm tired of scratching my head.
For two classes:
class User(Base):
__tablename__='users'
id = Column(Integer, primary_key=True)
name = Column(String(32))
...
class UserPost(Base):
__tablename__='posts'
id = Column(Integer, primary_key=True)
poster = Column(Integer, ForeignKey('users.id'))
subject = Column(String(32))
What I need is a method for:
post = session.query(UserPost).filter_by(subject="foo").one()
print post.poster.name
>>> "John Doe"
I tried with this attribute relation(), but I just kept spinning around with errors regarding union relationships, etc .: S
My relationship is like this:
class UserPost(Base):
__tablename__='posts'
id = Column(Integer, primary_key=True)
poster = Column(Integer, ForeignKey('users.id'))
subject = Column(String(32))
poster_user = relation(User, primaryjoin=poster==User.id)
I'm new to SQLAlchemy voodoo, so be gentle! :)
We thank the guys in advance and apologize in advance if this turns into RTFM or the wrong end of the stick
source
share