コード例 #1
0
class Subject(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    name = db.Column(db.String(32))
コード例 #2
0
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"))
    """
コード例 #3
0
class Project(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    modules = db.relationship("Module", backref='project')
コード例 #4
0
class Module(db.Model):
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey("project.id"))
コード例 #5
0

# 多对一
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)