flask中常见的关系模型定义
一对多
应用场景:角色与所属于该角色的用户(角色表与多用户表)
01
02
03
04
05
06
07
08
09
10
|
class
Role(db.Model):
__tablename__
=
'roles'
id
=
db.Column(db.Integer, primary_key
=
True
)
name
=
db.Column(db.String(
64
), unique
=
True
)
users
=
db.relationship(
'User'
, backref
=
'role'
, lazy
=
'dynamic'
)
class
User(db.Model):
__tablename__
=
'users'
id
=
db.Column(db.Integer, primary_key
=
True
)
name
=
db.Column(db.String(
64
), unique
=
True
, index
=
True
)
|
relationship函数:sqlalchemy对关系之间提供的一种便利的调用方式,关联不同的表
backref:在关系的另一模型中添加反向引用
lazy参数:决定了 SQLAlchemy 什么时候从数据库中加载数据,有四个可选方式:'select','joined','subquery','dynamic':
'select'(默认值):SQLAlchemy 会在使用一个标准 select 语句时一次性加载数据;
'joined':让 SQLAlchemy 当父级使用 JOIN 语句是,在相同的查询中加载关系;
'subquery':类似 'joined' ,但是 SQLAlchemy 会使用子查询;
'dynamic':SQLAlchemy 会返回一个查询对象,在加载这些条目时才进行加载数据,大批量数据查询处理时推荐使用。
一对一
应用场景:用户与该用户对应的地址信息
01
02
03
04
05
06
07
08
09
10
|
class
User(db.Model):
__tablename__
=
'users'
id
=
db.Column(db.Integer, primary_key
=
True
)
name
=
db.Column(db.String(
64
), unique
=
True
, index
=
True
)
address
=
relationship(
"Address"
, backref
=
backref(
"user"
, uselist
=
False
))
class
Address(db.Model):
__tablename__
=
'users'
id
=
db.Column(db.Integer, primary_key
=
True
)
name
=
db.Column(db.String(
64
))
|
在一对多关系基础上的用户表中使用backref函数,并添加uselist参数来表示一对一关系。
多对多
应用场景:学生与其选修的课程(学生表与选修课程表)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
tb_student_course
=
db.Table(
'tb_student_course'
,
db.Column(
'student_id'
, db.Integer, db.ForeignKey(
'students.id'
)),
db.Column(
'course_id'
, db.Integer, db.ForeignKey(
'courses.id'
))
)
class
Student(db.Model):
__tablename__
=
"students"
id
=
db.Column(db.Integer, primary_key
=
True
)
name
=
db.Column(db.String(
64
), unique
=
True
)
courses
=
db.relationship(
'Course'
, secondary
=
tb_student_course,
backref
=
db.backref(
'students'
, lazy
=
'dynamic'
),
lazy
=
'dynamic'
)
class
Course(db.Model):
__tablename__
=
"courses"
id
=
db.Column(db.Integer, primary_key
=
True
)
name
=
db.Column(db.String(
64
), unique
=
True
)
|
secondary:指定多对多关系中关系表的名字
backref:在关系的另一模型中添加反向引用,并且决定反向查询时什么时候从数据库中加载数据
自关联一对多
应用场景:评论与该评论的子评论(评论表)
01
02
03
04
05
06
07
08
09
10
|
class
Comment(db.Model):
__tablename__
=
"comments"
id
=
db.Column(db.Integer, primary_key
=
True
)
# 评论内容
content
=
db.Column(db.Text, nullable
=
False
)
# 父评论id
parent_id
=
db.Column(db.Integer, db.ForeignKey(
"comments.id"
))
# 父评论(也是评论模型)
parent
=
db.relationship(
"Comment"
, remote_side
=
[
id
],
backref
=
db.backref(
'childs'
, lazy
=
'dynamic'
))
|
remote_side:指定自关联的关系字段,为当前表主键
自关联多对多
应用场景:用户关注其他用户(用户表,中间表)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
tb_user_follows
=
db.Table(
"tb_user_follows"
,
db.Column(
'follower_id'
, db.Integer, db.ForeignKey(
'info_user.id'
), primary_key
=
True
),
# 粉丝id
db.Column(
'followed_id'
, db.Integer, db.ForeignKey(
'info_user.id'
), primary_key
=
True
)
# 被关注人的id
)
class
User(db.Model):
__tablename__
=
"info_user"
id
=
db.Column(db.Integer, primary_key
=
True
)
name
=
db.Column(db.String(
32
), unique
=
True
, nullable
=
False
)
# 用户所有的粉丝,添加了反向引用followed,代表用户都关注了哪些人
followers
=
db.relationship(
'User'
,
secondary
=
tb_user_follows,
primaryjoin
=
id
=
=
tb_user_follows.c.followed_id,
secondaryjoin
=
id
=
=
tb_user_follows.c.follower_id,
backref
=
db.backref(
'followed'
, lazy
=
'dynamic'
),
lazy
=
'dynamic'
)
|
primaryjoin:明确指定两个模型之间使用的关系条件
secondaryjoin:在SQLAlchemy中无法自行决定时,指定多对多关系中的二级关系条件
更多技术资讯可关注:gzitcast