迁移失败了会怎样?回滚能把数据找回来吗?为什么不同数据库上失败后的状态不一样?这篇用真实的 PostgreSQL 与 SQLite 把几种失败与回滚的结局放在一起对照。
upgrade 后: [('ann', 42)]
downgrade 后的列: ['id', 'name']
再 upgrade 后: [('ann', None)] (score 的值丢了)上一篇:Flask-Migrate Migration 文件;下一篇:SQLite 与 PostgreSQL 行为差异。
[pg] upgrade 退出码 1:sqlalchemy.exc.IntegrityError: (psycopg.errors.NotNullViolation) column "b_col" of relation "user" contains null values [pg] 失败之后的列: ['id', 'name'] | alembic_version: r001 [pg] 修好问题前直接再 upgrade 一次 → 退出码 1:sqlalchemy.exc.IntegrityError: (psycopg.errors.NotNullViolation) column "b_col" of relation "user" contains null values [sqlite] upgrade 退出码 1:sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL [sqlite] 失败之后的列: ['id', 'name', 'a_col'] | alembic_version: ['r001'] [sqlite] 修好问题前直接再 upgrade 一次 → 退出码 1:sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) duplicate column name: a_col
downgrade → 退出码 1: ERROR [flask_migrate] Error: 这个迁移不可逆:原来的大小写已经丢了 版本记录仍在 r002: ['r002']
upgrade 后版本: ['r003'] 列: ['id', 'name', 'c2', 'c3'] downgrade -- -1 后: ['r002'] ['id', 'name', 'c2'] downgrade r001 后: ['r001'] ['id', 'name'] downgrade base 后: [] | 表: ['alembic_version']