问题1:当新增模子类后执行alembic revision --autogenerate没有天生迁移脚本,最可能的原因是?
A. 忘记保存模子文件
B. 模子未正确导入到env.py
C. 数据库连接失败
D. 未安装sqlalchemy 答案:B。Alembic需要正确导入包含Base类的模子定义才能进行元数据比对,假如未在env.py中正确设置target_metadata,会导致无法检测模子变化。 问题2:哪个命令可以查看当前数据库版本?
A. alembic current
B. alembic show
C. alembic history
D. alembic heads 答案:A。alembic current命令显示当前数据库所处的迁移版本。
1.6 常见报错解决方案
错误1:FAILED: Target database is not up to date
alembic upgrade head
复制代码
原因:存在未应用的迁移版本 解决:执行升级命令更新数据库 错误2:SAWarning: Did not recognize type 'geometry'...
# 在env.py中添加自定义类型映射
from sqlalchemy.dialects.postgresql import JSONB
def include_name(name, type_, parent_names):
if type_ == "geometry":
return False
return True
context.configure(
...
include_name = include_name,
user_module_prefix = 'sa.'
)
复制代码
原因:利用了数据库特定的字段范例 解决:在env.py中添加范例过滤逻辑 错误3:Can't locate revision identified by 'xxxxx'