- SQLAlchemy 教程
- SQLAlchemy - 主页
- SQLAlchemy - 简介
- SQLAlchemy 核心
- 表达语言
- 连接到数据库
- 创建表
- SQL 表达式
- 执行表达式
- 选择行
- 使用文本 SQL
- 使用别名
- 使用 UPDATE 表达式
- 使用 DELETE 表达式
- 使用多个表
- 使用多个表更新
- 按参数顺序更新
- 多表删除
- 使用连接
- 使用连词
- 使用函数
- 使用集合运算
- SQLAlchemy ORM
- 声明映射
- 创建会话
- 添加对象
- 使用查询
- 更新对象
- 应用过滤器
- 过滤器运算符
- 返回列表和标量
- 文本SQL
- 建立关系
- 使用相关对象
- 使用连接
- 常见关系运算符
- 急切加载
- 删除相关对象
- 多对多关系
- 方言
- SQLAlchemy 有用资源
- SQLAlchemy - 快速指南
- SQLAlchemy - 有用的资源
- SQLAlchemy - 讨论
SQLAlchemy ORM - 删除相关对象
对单个表进行删除操作很容易。您所要做的就是从会话中删除映射类的对象并提交操作。然而,对多个相关表进行删除操作有点棘手。
在我们的 sales.db 数据库中,客户和发票类映射到客户和发票表,具有一对多类型的关系。我们将尝试删除 Customer 对象并查看结果。
作为快速参考,以下是客户和发票类的定义 -
from sqlalchemy import create_engine, ForeignKey, Column, Integer, String engine = create_engine('sqlite:///sales.db', echo = True) from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() from sqlalchemy.orm import relationship class Customer(Base): __tablename__ = 'customers' id = Column(Integer, primary_key = True) name = Column(String) address = Column(String) email = Column(String) class Invoice(Base): __tablename__ = 'invoices' id = Column(Integer, primary_key = True) custid = Column(Integer, ForeignKey('customers.id')) invno = Column(Integer) amount = Column(Integer) customer = relationship("Customer", back_populates = "invoices") Customer.invoices = relationship("Invoice", order_by = Invoice.id, back_populates = "customer")
我们设置一个会话并通过使用以下程序使用主 ID 查询来获取 Customer 对象 -
from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=engine) session = Session() x = session.query(Customer).get(2)
在我们的示例表中,x.name 恰好是“Gopal Krishna”。让我们从会话中删除这个 x 并计算这个名称的出现次数。
session.delete(x) session.query(Customer).filter_by(name = 'Gopal Krishna').count()
结果 SQL 表达式将返回 0。
SELECT count(*) AS count_1 FROM ( SELECT customers.id AS customers_id, customers.name AS customers_name, customers.address AS customers_address, customers.email AS customers_email FROM customers WHERE customers.name = ?) AS anon_1('Gopal Krishna',) 0
但是,x 的相关 Invoice 对象仍然存在。可以通过以下代码验证 -
session.query(Invoice).filter(Invoice.invno.in_([10,14])).count()
此处,10 和 14 是属于客户 Gopal Krishna 的发票号码。上述查询结果为2,表示相关对象没有被删除。
SELECT count(*) AS count_1 FROM ( SELECT invoices.id AS invoices_id, invoices.custid AS invoices_custid, invoices.invno AS invoices_invno, invoices.amount AS invoices_amount FROM invoices WHERE invoices.invno IN (?, ?)) AS anon_1(10, 14) 2
这是因为 SQLAlchemy 不假设级联的删除;我们必须发出命令才能删除它。
要更改Behave,我们在 User.addresses 关系上配置级联选项。让我们关闭正在进行的会话,使用新的 declarative_base() 并重新声明 User 类,添加地址关系(包括级联配置)。
关系函数中的级联属性是一个以逗号分隔的级联规则列表,它确定会话操作应如何从父级“级联”到子级。默认情况下为False,表示是“保存-更新、合并”。
可用的级联如下 -
- 保存更新
- 合并
- 删除
- 删除
- 删除孤儿
- 刷新过期
经常使用的选项是“all,delete-orphan”,表示相关对象在所有情况下都应跟随父对象,并在取消关联时删除。
因此,重新声明的客户类别如下所示 -
class Customer(Base): __tablename__ = 'customers' id = Column(Integer, primary_key = True) name = Column(String) address = Column(String) email = Column(String) invoices = relationship( "Invoice", order_by = Invoice.id, back_populates = "customer", cascade = "all, delete, delete-orphan" )
让我们使用以下程序删除名为 Gopal Krishna 的客户,并查看其相关发票对象的计数 -
from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind = engine) session = Session() x = session.query(Customer).get(2) session.delete(x) session.query(Customer).filter_by(name = 'Gopal Krishna').count() session.query(Invoice).filter(Invoice.invno.in_([10,14])).count()
上面脚本发出的 SQL 语句现在计数为 0 -
SELECT customers.id AS customers_id, customers.name AS customers_name, customers.address AS customers_address, customers.email AS customers_email FROM customers WHERE customers.id = ? (2,) SELECT invoices.id AS invoices_id, invoices.custid AS invoices_custid, invoices.invno AS invoices_invno, invoices.amount AS invoices_amount FROM invoices WHERE ? = invoices.custid ORDER BY invoices.id (2,) DELETE FROM invoices WHERE invoices.id = ? ((1,), (2,)) DELETE FROM customers WHERE customers.id = ? (2,) SELECT count(*) AS count_1 FROM ( SELECT customers.id AS customers_id, customers.name AS customers_name, customers.address AS customers_address, customers.email AS customers_email FROM customers WHERE customers.name = ?) AS anon_1('Gopal Krishna',) SELECT count(*) AS count_1 FROM ( SELECT invoices.id AS invoices_id, invoices.custid AS invoices_custid, invoices.invno AS invoices_invno, invoices.amount AS invoices_amount FROM invoices WHERE invoices.invno IN (?, ?)) AS anon_1(10, 14) 0