Hello everyone,
To anyone with experience of SQLAlchemy, this will be basic I am sure; But I don't find the docs that helpful and I am sick of scratching my head.
Given 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 am after is a method for:
post = session.query(UserPost).filter_by(subject="foo").one()
print post.poster.name
>>> "John Doe"
I was attempting this with a relation()
attribute, but I just kept going round in circles with errors regarding relationship of joins and so on :S
My Relation looks like:
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 am new to the voodoo of SQLAlchemy so be gentle! :)
Thanks in advance guys, and apologies in advance if this turns into a RTFM or wrong-end-of-stick