【问题标题】:SQLAlchemy - Self referential Many-to-many relationship with extra columnSQLAlchemy - 与额外列的自引用多对多关系
【发布时间】:2014-09-30 09:30:08
【问题描述】:

我有一个代表用户的模型,我想在代表他们是朋友的用户之间创建关系。我的带有关联表和列出所有朋友的方法的功能模型如下所示

friendship = db.Table('friend',
    db.Column('id', db.Integer, primary_key=True),
    db.Column('fk_user_from', db.Integer, db.ForeignKey('user.id'), nullable=False),
    db.Column('fk_user_to', db.Integer, db.ForeignKey('user.id'), nullable=False)
)

class User(db.Model):
   ...
   ...
   friends = db.relationship('User',
        secondary=friendship,
        primaryjoin=(friendship.c.fk_user_from==id),
        secondaryjoin=(friendship.c.fk_user_to==id),
        backref = db.backref('friend', lazy = 'dynamic'), 
        lazy = 'dynamic')

    def list_friends(self):
        friendship_union = db.select([
                        friendship.c.fk_user_from, 
                        friendship.c.fk_user_to
                        ]).union(
                            db.select([
                                friendship.c.fk_user_to, 
                                friendship.c.fk_user_from]
                            )
                    ).alias()
        User.all_friends = db.relationship('User',
                       secondary=friendship_union,
                       primaryjoin=User.id==friendship_union.c.fk_user_from,
                       secondaryjoin=User.id==friendship_union.c.fk_user_to,
                       viewonly=True) 
        return self.all_friends

问题是我需要在确认之前实现询问友谊和待定状态(就像你从 Facebook 知道的那样),所以有必要在友谊表中添加一个额外的列。根据SQLAlchemy tutorial,我应该创建一个关联对象,但如何让它再次自引用?

或者是否可以将此列添加到我当前的友谊表中并以某种方式访问​​和更改那里的状态值?

谢谢

【问题讨论】:

    标签: python database database-design sqlalchemy flask-sqlalchemy


    【解决方案1】:

    所有你需要的是在你的表中添加primaryjoin,并在友谊表中创建两个外键,'primary_key'。您还需要在班级中建立友谊。

    class Friendship(db.Model):
        __tablename__ = 'friend'
        fk_user_from = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
        fk_user_to = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
        extra_field = db.Column(db.Integer)
    
    
    class User (db.Model):
        __tablename__ = 'user'
        id = db.Column(db.Integer, primary_key=True)
        user_to = db.relationship('Friendship',backref='to', primaryjoin=id==Friendship.fk_user_to)
        user_from = db.relationship('Friendship',backref='from', primaryjoin=id==Friendship.fk_user_from )
    

    要添加朋友,您需要像这样定义友谊:

    friend = Friendship(extra_field=0 , to=me , from=my_friend)
    

    【讨论】:

    • 我已经用同样的方法解决了,但感谢您的回复:) 接受
    • 显然,最后一行不起作用。我必须改用friend = Friendship(extra_field=0 , fk_user_to=me , fk_user_from=my_friend)
    • 对我来说,这引发了sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join ... tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.
    【解决方案2】:

    我已经回答了一个类似的问题,该问题解决了与关联对象的自引用多对多关系。

    https://stackoverflow.com/a/62281276/9387542

    另外,如果你只是想建立一个友谊模型,我建议你和https://stackoverflow.com/a/7317251/9387542的模型建立多对多关系

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-18
      • 2016-08-01
      • 2023-02-02
      • 2019-10-16
      • 2021-05-31
      • 2018-10-02
      • 2011-05-09
      • 1970-01-01
      相关资源
      最近更新 更多