25

难以理解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

  1. 检查平等primaryjoin的目的是什么?或者,换句话说,secondaryjoin如何将s 添加到关联表中?primaryjoinsecondaryjoinuser.id
  2. 既然两者都primaryjoinsecondaryjoin一个user.id要求,那user.id该去哪儿呢?
  3. 在我的关注/取消关注方法中,SQLAlchemy 如何知道自己是关注者,而传入的用户是被关注的用户?

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

4

1 回答 1

26
  1. 在多对多关系中,primaryjoin 表达式描述左表和联结表之间的连接,辅助连接描述联结表和右表之间的连接。换句话说,primaryjoin 表达式是说,“查找followers 表中follower_id 为X 的所有行”,secondaryjoin 表达式是说“查找followers 表中followers_id 为X 的所有行”,然后将这两者放在一起找到关注用户 X 的所有用户,以及用户 X 关注的所有用户。

  2. 这取决于您查询的方向。当您请求 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。

  3. 因为您将它添加到 self.followed 集合中,所以告诉 SQLAlchemy 有人 self 正在关注。如果你将它添加到 self.followers 集合中,你会做相反的事情,告诉 SQLAlchemy 'user' 是 self 的追随者。

参考:用于指定替代连接条件primaryjoinsecondaryjoin)的 SQLAlchemy 文档。

于 2013-11-01T22:01:38.050 回答