美文网首页
flask的orm框架(SQLAlchemy)-一对多查询以及多

flask的orm框架(SQLAlchemy)-一对多查询以及多

作者: 忘了呼吸的那只猫 | 来源:发表于2019-09-17 12:19 被阅读0次

- 一对多,多对多是什么?

一对多。例如,班级与学生,一个班级对应多个学生,或者多个学生对应一个班级。
多对多。例如,学生与课程,可以有多个学生修同一门课,同时,一门课也有很多学生。

- 一对多查询

如果一个项目,有两张表。分别是班级表,学生表。

在设计数据表时,我们给学生表设置一个外键,指向班级表的 id 。

sqlalchemy 模板创建表的代码:

from flask import Flask, render_template, request, flash, redirect
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__,static_folder="static",template_folder="templates")

# 设置数据库连接属性
app.config['SQLALCHEMY_DATABASE_URI'] = '×××'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# 实例化 ORM 操作对象
db = SQLAlchemy(app)

# 班级表
class Classes(db.Model):
    __tablename__ = "classes"
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(20),nullable=False,unique=True)

# 学生表
class Students(db.Model):
    __tablename__ = "students"
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(40),nullable=False)
    cls_id = db.Column(db.Integer,db.ForeignKey("classes.id"))    # 注意要写成(表名.字段名)
注意:代码中的‘xxx’需要改成你的数据库URI

创建完表,插入完数据后。

如果我们知道学生的学号,要查学生班级的名称,应该怎么操作呢?

现在可以用一种比较麻烦的方达查询:

cls_id = Students.query.filter(Student.id == 'xxx').first()
cls = Classes.query.filter(Classes.id == cls.id).first()
print(cls.name)

这样的方法太麻烦了,有没有简单的办法?

上面创建表的代码,在18行可以插入一条语句。

relate_student = db.relationship("Students",backref='relate_class',lazy='dynamic')

其中realtionship描述了StudentsClasses的关系。在此文中,第一个参数为对应参照的类"Students"

  • 第二个参数backref为类Students申明新属性的方法
  • 第三个参数lazy决定了什么时候SQLALchemy从数据库中加载数据
  • 如果设置为子查询方式(subquery),则会在加载完Classes对象后,就立即加载与其关联的对象,这样会让总查询数量减少,但如果返回的条目数量很多,就会比较慢
  • 另外,也可以设置为动态方式(dynamic),这样关联对象会在被使用的时候再进行加载,并且在返回前进行过滤,如果返回的对象数很多,或者未来会变得很多,那最好采用这种方式

如果一大堆理论看不明白,那么知道怎么用就可以了。

如果知道学生的姓名,想知道班级的名称,可以这样查:

stu = Students.query.filter(Students.name == 'xxx').first()
stu.relate_class.name    # stu.relate_class 会跳到 classes 表

如果知道班级的名称,想返回全部学生的名字的列表,可以这样查:

cls = Classes.query.filter(Classes.name == 'xxx').first()
cls.relate_student.first().name    # cls.relate_stu 会跳到 students 表
注意:在设置db.relationship的一方查询时需要用first()或者all()才能获取到对象,所以一对多的关系中尽可能把外键设置在多的一方,db.relationship设置在‘一’的一方。

- 多对多查询

假设一堆学生选了不同的课程,这就是多对多关系。

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)

   # 关联属性,多对多的情况,可以写在任意一个模型类中
    relate_courses = db.relationship('Course', secondary=tb_student_course,
                              backref='relate_student',
                              lazy='dynamic')


class Course(db.Model):
    __tablename__ = "courses"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)

添加测试数据:

# 添加测试数据

    stu1 = Student(name='张三')
    stu2 = Student(name='李四')
    stu3 = Student(name='王五')

    cou1 = Course(name='物理')
    cou2 = Course(name='化学')
    cou3 = Course(name='生物')

    stu1.courses = [cou2, cou3]    # 记得要添加关系
    stu2.courses = [cou2]
    stu3.courses = [cou1, cou2, cou3]

    db.session.add_all([stu1, stu2, stu2])
    db.session.add_all([cou1, cou2, cou3])

    db.session.commit()

要查某个学生修的全部课程,修了某个课程的全部学生:

for course in stu1.relate_courses.all():
    print(course.name)

for student in cou2.relate_student.all():
    print(student)

特殊(自关联/自引用)

在 web 开发中还有一种情况,就是同一张表通过关系列表实现自关联。常见例子入微博用户的 “关注者/粉丝”(followed/followers)。


数据库示意图

实现代码:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///demo.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
migrate = Migrate(app, db)


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


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    followed = db.relationship(
        # User 表示关系当中的右侧实体(将左侧实体看成是上级类)
        # 由于这是自引用关系,所以两侧都是同一个实体。
        'User', 

        # secondary 指定了用于该关系的关联表
        # 就是使用我在上面定义的 followers
        secondary=followers,

        # primaryjoin 指明了右侧对象关联到左侧实体(关注者)的条件 
        # 也就是根据左侧实体查找出对应的右侧对象
        # 执行 user.followed 时候就是这样的查找
        primaryjoin=(followers.c.follower_id == id),

        # secondaryjoin 指明了左侧对象关联到右侧实体(被关注者)的条件 
        # 也就是根据右侧实体找出左侧对象
        # 执行 user.followers 时候就是这样的查找
        secondaryjoin=(followers.c.followed_id == id),

        # backref 定义了右侧实体如何访问该关系
        # 也就是根据右侧实体查找对应的左侧对象
        # 在左侧,关系被命名为 followed
        # 在右侧使用 followers 来表示所有左侧用户的列表,即粉丝列表
        backref=db.backref('followers', lazy='dynamic'), 
        lazy='dynamic'
        )      

@app.shell_context_processor
def make_shell_context():
    return {'db': db, 'User': User}

关联关系也可以分开写

class Follow(db.Model):
    __tablename__ = 'follows'
    followed_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
    follower_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)


class Follow(db.Model):
    __tablename__ = 'follows'
    followed_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
    follower_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    followed = db.relationship('Follow', foreign_keys=[Follow.follower_id],
                               backref=db.backref('follower',lazy='joined'),
                               lazy='dynamic',
                               cascade='all, delete-orphan')
    follower = db.relationship('Follow', foreign_keys=[Follow.followed_id],
                               backref=db.backref('followed',lazy='joined'),
                               lazy='dynamic',
                               cascade='all, delete-orphan')

假设 followers 表如下数据:

follower_id followed_id
1 2
1 3
2 3

这表格表示 id 为 1 的用户(称为:user1)关注了(followed) id 为 2、3 的用户(称为:user2、user3),user2 关注了 user3。
反过来说,user1 是 user2、user3 的关注者(followers)。
当我们执行 user1.followed 的操作,是根据左侧实体查找出对应的右侧对象,查询条件为 followers.c.follower_id == 1,得到 user2 和 user3,也就是 user1 关注的用户。

>>> u1 = User.query.get(1)
>>> u1.followed.all()
[<User 2>, <User 3>]

当我们执行 user3.followers 的操作,是根据右侧实体找出左侧对象,查询条件为 followers.c.followed_id == 3,得到 user1、user2,也就是 use3 的关注者。

>>> u3 = User.query.get(3)
>>> u3.followers.all()
[<User 1>, <User 2>]

关注和取消关注
user1 关注 user2:

user1.followed.append(user2)

user1 取消关注 user2:

user1.followed.remove(user2)

更有效的方法是我们在 User 类的方法里集成关注和取消关注:

class User(UserMixin, db.Model):
    #...

    def is_following(self, user):
        """
        检查是否已关注 user
        """
        return self.followed.filter(
            followers.c.followed_id == user.id).count() > 0

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)

相关文章

网友评论

      本文标题:flask的orm框架(SQLAlchemy)-一对多查询以及多

      本文链接:https://www.haomeiwen.com/subject/hpxqyctx.html