Saturday, 14 September 2013

sqlalchemy foreign key relationship attributes

sqlalchemy foreign key relationship attributes

I have a User table and a Friend table. The Friend table holds two foreign
keys both to my User table as well as a status field. I am trying to be
able to call attributes from my User table on a Friend object. For
example, I would love to be able to do something like, friend.name, or
friend.email.
class User(Base):
""" Holds user info """
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(25), unique=True)
email = Column(String(50), unique=True)
password = Column(String(25))
admin = Column(Boolean)
# relationships
friends = relationship('Friend', backref='Friend.friend_id',
primaryjoin='User.id==Friend.user_id', lazy='dynamic')
class Friend(Base):
__tablename__ = 'friend'
user_id = Column(Integer, ForeignKey(User.id), primary_key=True)
friend_id = Column(Integer, ForeignKey(User.id), primary_key=True)
request_status = Column(Boolean)
When I get friend objects all I have is the 2 user_ids and i want to
display all properties of each user so I can use that information in
forms, etc. I am not to sqlalchemy - still trying to learn more advanced
features. This is just a snippet from a larger Flask project and this
feature is going to be for 'friend' requests, etc. I've tried to look up
association objects, etc, but I am having a hard with it.
Any help would be greatly appreciated.

No comments:

Post a Comment