sqlalchemy外键关系属性 [英] sqlalchemy foreign key relationship attributes

查看:236
本文介绍了sqlalchemy外键关系属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表和一个朋友表.朋友表拥有两个外键,分别指向我的用户表和状态字段.我试图能够从Friend对象上的User表中调用属性.例如,我希望能够执行诸如friend.name或friend.email之类的操作.

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)

当我得到friend个对象时,我所拥有的只是2个user_ids,并且我想显示每个用户的所有属性,以便可以在表单中使用该信息,等等.更高级的功能.这只是一个更大的Flask项目中的一个片段,此功能将用于friend请求等.我试图查找关联对象等,但是我很难解决.

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.

任何帮助将不胜感激.

Any help would be greatly appreciated.

推荐答案

首先,如果您使用的是flask-sqlalchemy,为什么要直接使用sqlalchemy而不是Flask的db.Model?

First, if you're using flask-sqlalchemy, why are you using directly sqlalchemy instead of the Flask's db.Model?

我强烈建议使用 flask-sqlalchemy 扩展名,因为它利用了会话和其他一些巧妙的东西

I strongly reccomend to use flask-sqlalchemy extension since it leverages the sessions and some other neat things.

创建代理便捷对象很简单.只需在Friend类中添加关系即可.

Creating a proxy convenience object is straightforward. Just add the relationship with it in the Friend class.

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将负责其余的工作,您可以通过以下方式简单地访问用户对象:

SQLAlchemy will take care of the rest and you can access the user object simply by:

name = friend.user.name

如果您计划每次使用friend对象时都使用user对象,请在relationship中指定lazy='joined'.这样,它可以在单个查询中加载两个对象.

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.

这篇关于sqlalchemy外键关系属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆