2

Below I copied an example from the flask Megatutorial where users can follow other users and can be followed by other users. My question is how can I modify this, so that each user has the attribute 'connections', which returns all users you follow or who follow you? Basically removing the separation between users who follow you an users you follow.

Here is the table for that relationship

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

and here is the definition for the relationship

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')
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
carl
  • 4,216
  • 9
  • 55
  • 103
  • 1
    Please check if you can find your answer here: http://stackoverflow.com/questions/9116924/how-can-i-achieve-a-self-referencing-many-to-many-relationship-on-the-sqlalchemy – AKS May 01 '16 at 13:23

0 Answers0