简体   繁体   English

SQLAlchemy - 与额外列的自引用多对多关系

[英]SQLAlchemy - Self referential Many-to-many relationship with extra column

I have a model representing user and I want to create a relationship between users representing that they are friends.我有一个代表用户的模型,我想在代表他们是朋友的用户之间建立关系。 My functional model with association table and methods to list all the friends look like this我的带有关联表和列出所有朋友的方法的功能模型如下所示

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

The problem is that I need to implement asking for frienship and status pending before confirmation (just like you know it from Facebook) so it is necesarry to add an extra column to the frienship table.问题是我需要在确认之前实现询问好友和状态待定(就像你从 Facebook 知道的那样),所以需要在好友表中添加一个额外的列。 According to the SQLAlchemy tutorial I should create an Association Object but how to make it self referential again?根据SQLAlchemy 教程,我应该创建一个关联对象,但如何使其再次自引用?

Or is it possible to just add this column to my current frienship table and access and change the status value there somehow?或者是否可以将此列添加到我当前的朋友表中并以某种方式访问​​和更改状态值?

Thanks谢谢

All you need is to add primaryjoin in your table and also making two foreignkey in table of Friendship, 'primary_key' too.您所需要的只是在您的表中添加 primaryjoin 并在 Friendship 表中创建两个外键,“primary_key”。 you also need to make friendship as a class.你还需要把友谊当成一个班级。

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 )

And to add a friend you need to define Friendship like:要添加一个朋友,您需要像这样定义 Friendship:

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

I have answered to a similar question that solves Self-referential many-to-many relationship with Association Object.我已经回答了一个类似的问题,该问题解决了与关联对象的自引用多对多关系。

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

Also, if you solely looking for establing a frienship model, I suggest you to establish Many-to-many relationship with the model from https://stackoverflow.com/a/7317251/9387542另外,如果您只是在寻找建立友谊模型,我建议您与来自https://stackoverflow.com/a/7317251/9387542的模型建立多对多关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sqlalchemy:自引用多对多关系与关系表中的额外列 - Sqlalchemy: self-referential many-to-many relationship with extra column in relationship table 自引用多对多关系与关联 object 中的额外列 - Self referencing many-to-many relationship with extra column in association object 通过SqlAlchemy中的关联对象实现多对多,自引用,非对称关系(推特模型) - Many-to-many, self-referential, non-symmetrical relationship (twitter model) via Association Object in SqlAlchemy 如何在SQLAlchemy中声明对称自引用多对多关系用于存储有向图 - How to declare symmetric self-referential many-to-many relationship in SQLAlchemy for storing directed graph 级联删除自指多对多关系 - Cascade delete from self-referential many-to-many relationship SQLAlchemy具有额外关联数据的自参考多对多 - SQLAlchemy self-referential many to many with extra association data SQLAlchemy自引用多对多对称关系 - SQLAlchemy Self-Referential Many to Many Symmetric Relationship SQLAlchemy自引用多对一关系:示例 - SQLAlchemy self-referential many-to-one relationship: example 使用WTForms,SQLAlchemy和Flask与额外字段进行多对多关系 - Many-to-many relationship with extra fields using WTForms, SQLAlchemy and Flask SQLAlchemy-将自引用关系一对多映射(声明形式) - SQLAlchemy - Mapping self-referential relationship as one to many (declarative form)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM