sqlite-utils 4.0 于 2026 年 7 月 7 日发布,新增三大特性:数据库模式迁移、通过新的 db.atomic() 方法实现的嵌套事务,以及对复合外键的支持。 此版本显著增强了 sqlite-utils 作为 SQLite 数据库管理工具的能力,满足了模式版本控制和事务安全性的常见需求。使用 Python 进行数据管理的开发者将受益于更简便的迁移工作流和更健壮的事务处理。 迁移通过 Python 文件中的 Migrations 类和 table.transform() 方法定义,该方法实现了 SQLite 推荐的临时表模式。嵌套事务底层使用 SQLite 保存点,复合外键则支持引用多个列。
背景
SQLite 是一个嵌入式 SQL 数据库引擎,本身不支持模式迁移或嵌套事务。sqlite-utils 库由 Simon Willison 创建,提供 Python API 和命令行工具来操作 SQLite 数据库。在 4.0 版本之前,用户需要手动使用 SQLite 有限的 ALTER TABLE 功能来处理模式更改。此版本自动化了迁移跟踪和执行,并通过保存点引入了嵌套事务。
参考链接
- Transaction - SQLite java - SQLiteDatabase nested transaction and workaround ... Code sample How to Handle Nested Transactions in SQLite - Sling Academy Understanding Nested Transactions in SQLite and Effective ... How to use transactions — sqlite7 documentation sqlite-utils 4.0rc1 adds migrations and nested transactions
- SQLite Foreign Key Support