SQLAlchemy - 自参照多对多与额外列的关系 [英] SQLAlchemy - Self referential Many-to-many relationship with extra column

查看:132
本文介绍了SQLAlchemy - 自参照多对多与额外列的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示用户的模型,我想在表示他们是朋友的用户之间创建一个关系。我的功能模型与关联表和方法列出所有的朋友看起来像这样

  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 = 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

问题是,我需要实现要求确认之前的frienship和状态待定(就像你从Facebook知道的那样),所以在frienship表中添加一个额外的列是必要的。根据 SQLAlchemy教程,我应该创建一个关联对象但是如何让它自我参照呢?



还是可以只是添加这一列到我目前的frienship表,访问和改变状态值在某种程度上? p>

感谢

解决方案

所有您需要的是在表中添加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 .id'),primary_key = True)
extra_field = db.Column(db.Integer)


类用户(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)


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. According to the SQLAlchemy tutorial I should create an Association Object but how to make it self referential again?

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. 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:

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

这篇关于SQLAlchemy - 自参照多对多与额外列的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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