Python sqlalchemy 外键关系属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18807322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sqlalchemy foreign key relationship attributes
提问by Ptrkcon
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.
我有一个 User 表和一个 Friend 表。Friend 表包含两个指向我的 User 表的外键以及一个状态字段。我试图能够从我的用户表中调用 Friend 对象上的属性。例如,我希望能够执行诸如friend.name 或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 new 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.
当我获得friend
对象时,我只有 2 user_ids
,我想显示每个用户的所有属性,以便我可以在表单等中使用该信息。我是 sqlalchemy 的新手 - 仍在尝试学习更高级的功能。这只是一个更大的 Flask 项目的一个片段,这个功能将用于friend
请求等。我试图查找关联对象等,但我很难使用它。
Any help would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by Paolo Casciello
First, if you're using flask-sqlalchemy
, why are you using directly sqlalchemy instead of the Flask's db.Model
?
首先,如果您使用的是flask-sqlalchemy
,为什么要直接使用 sqlalchemy 而不是 Flask 的db.Model
?
I strongly reccomend to use flask-sqlalchemy
extension since it leverages the sessions and some other neat things.
我强烈建议使用flask-sqlalchemy
扩展,因为它利用了会话和其他一些整洁的东西。
Creating a proxy convenience object is straightforward. Just add the relationship with it in the Friend
class.
创建代理便利对象很简单。只需在Friend
类中添加与它的关系即可。
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)
user = relationship('User', foreign_keys='Friend.user_id')
friend = relationship('User', foreign_keys='Friend.friend_id')
SQLAlchemy will take care of the rest and you can access the user object simply by:
SQLAlchemy 将负责其余的工作,您可以简单地通过以下方式访问用户对象:
name = friend.user.name
If you plan to use the user
object every time you use the friend
object specify lazy='joined'
in the relationship
. This way it loads both object in a single query.
如果您打算在user
每次使用对象时都使用该friend
对象 lazy='joined'
,请在relationship
. 这样,它会在单个查询中加载两个对象。