问题描述
- 学习《flask web开发》中多对多关系时时遇到问题
-
class Follow(db.Model): __tablename__ = 'follows' follower_id = db.Column(db.Integer db.ForeignKey('users.id') primary_key=True) followed_id = db.Column(db.Integer db.ForeignKey('users.id') primary_key=True) timestamp = db.Column(db.DateTime default=datetime.utcnow)class User(UserMixin db.Model): __tablename__ = 'users' id = db.Column(db.Integer primary_key=True) email = db.Column(db.String(64) unique=True index=True) username = db.Column(db.String(64) unique=True index=True) password_hash = db.Column(db.String(128)) confirmed = db.Column(db.Boolean default=False) name = db.Column(db.String(64)) location = db.Column(db.String(64)) about_me = db.Column(db.Text()) #头像哈希 avatar_hash = db.Column(db.String(32)) posts = db.relationship('Post' backref='author' lazy='dynamic') followed = db.relationship('Follow' foreign_keys=[Follow.follower_id] backref=db.backref('follower' lazy='joined') lazy='dynamic' cascade='all delete-orphan') followers = db.relationship('Follow' foreign_keys=[Follow.followed_id] backref=db.backref('followed' lazy='joined') lazy='dynamic' cascade='all delete-orphan')
这里,User的followed的外键指定为Follow.follower_id 的意思是不是 ,该User对象的id对应的是followed中Follow对象中的follower_id 。这样理解有没有问题?还有其他参数都是什么意思?
时间: 2024-11-08 20:13:15