class Subject(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(32))
class User(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(32)) subjects = db.relationship("Subject", secondary=choiceClass, backref=db.backref("users", lazy="dynamic")) """
class Project(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) modules = db.relationship("Module", backref='project')
class Module(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) project_id = db.Column(db.Integer, db.ForeignKey("project.id"))
# 多对一 class Project(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) modules = db.relationship("Module", backref='project') class Module(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) project_id = db.Column(db.Integer, db.ForeignKey("project.id")) # 多对多 choiceClass = db.Table("choice", db.Column("user_id", db.Integer, db.ForeignKey('user.id')), db.Column("sub_id", db.Integer, db.ForeignKey('subject.ud')) ) class User(db.Model): id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(32)) subjects = db.relationship("Subject", secondary=choiceClass, backref=db.backref("users", lazy="dynamic")) """ lazy 默认懒加载 select user = User.query.get(1) user.subjects ==> [Subject()实例] dynamic user = User.query.get(1)