flask - 'primaryjoin' 和 'secondaryjoin' 如何处理 SQLAlchemy 中的多对多关系?

标签 flask sqlalchemy many-to-many flask-sqlalchemy

难以理解 Flask Mega Tutorial 中的一些 Flask-SQLAlchemy 内容.这是代码:

followers = db.Table('followers',
    db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
)

class User(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    nickname = db.Column(db.String(64), unique = True)
    email = db.Column(db.String(120), index = True, unique = True)
    role = db.Column(db.SmallInteger, default = ROLE_USER)
    posts = db.relationship('Post', backref = 'author', lazy = 'dynamic')
    about_me = db.Column(db.String(140))
    last_seen = db.Column(db.DateTime)
    followed = db.relationship('User', 
        secondary = followers, 
        primaryjoin = (followers.c.follower_id == id), 
        secondaryjoin = (followers.c.followed_id == id), 
        backref = db.backref('followers', lazy = 'dynamic'), 
        lazy = 'dynamic')

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)
            return self

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)
            return self

    def is_following(self, user):
        return self.followed.filter(followers.c.followed_id == user.id).count() > 0

所以我明白,因为这是一种自引用关系,我们需要某种方式让关联表确定表中的哪个用户是关注者,以及表中的哪个用户是被关注的用户。 Primaryjoinsecondaryjoin做到这一点,但如何?

关于 primaryjoin 我不明白的三件事和 secondaryjoin如下面所述:
  • primaryjoin的目的是什么?和 secondaryjoin检查平等?或者,换句话说,primaryjoin 到底是怎么回事?和 secondaryjoin添加 user.id s 到关联表?
  • 由于primaryjoinsecondaryjoin拍下user.id要求,其中 user.id去哪里?
  • 在我的关注/取消关注方法中,SQLAlchemy 如何知道 self 是关注者,而传入的用户是被关注的用户?

  • 这些问题一直阻碍我进入下一章,因此非常感谢任何答案。

    最佳答案

  • 在多对多关系中,primaryjoin 表达式描述左表和联结表之间的连接,secondaryjoin 描述联结表和右表之间的连接。换句话说,primaryjoin 表达式是说,“查找followers 表中follower_id 为X 的所有行”,secondaryjoin 表达式是说“查找followers 表中followers_id 为X 的所有行”,然后将这两者放在一起找到关注用户 X 的所有用户,以及用户 X 关注的所有用户。
  • 这取决于您查询的方向。当您请求 user.followers 时,它会通过使用 primaryjoin 来查询 follower_id == user.id 的所有行的 follower 表,并使用 other.id == follower_id 检索用户 other。当你请求 user.followed 时,它使用 secondaryjoin 查询 follower_id == user.id 的所有行的 follower 表,并检索 other.id == follow_id 的用户 other。
  • 因为您将它添加到 self.followed 集合中,所以告诉 SQLAlchemy 有人 self 正在关注。如果你将它添加到 self.followers 集合中,你会做相反的事情,告诉 SQLAlchemy 'user' 是 self 的追随者。

  • 引用:SQLAlchemy documentation for specifying alternative join conditions ( primaryjoinsecondaryjoin )。

    关于flask - 'primaryjoin' 和 'secondaryjoin' 如何处理 SQLAlchemy 中的多对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19598578/

    相关文章:

    python - SQL Alchemy - 如何从模型实例中删除?

    python - SQL Alchemy 使用带有 bindparam 的字典列表更新表不工作

    python - 用棉花糖序列化日期时间的简短方法

    node.js - sails.js 多对多查询

    vb.net - 如何为多对多关系插入实体? ( Entity Framework )

    python - Flask - Errno 13 权限被拒绝

    python - 无法加载模块

    hibernate - JPA2 多对多关系上的级联删除

    python - 将 MongoEngine 文档作为 JSON 返回

    python - 在本地主机上看不到我正在运行的Docker容器