我正在使用 Alembic 和 SQLAlchemy,但我是 Python 新手。
数据库:postgres 与 postgis(带有 Tiger schema 和其他)
我在 models.py 文件中创建了一个类来定义我的表。
class Adv(Base):
__tablename__ = 'advs'
__table_args__ = {"schema": "screech"}
id = Column(Integer, primary_key=True)
title = Column(String)
description = Column(String)
...
如果我跑步
alembic revision --autogenerate -m "Create table advs"
,在生成的文件中我可以看到很多这样的行op.drop_table('place', schema='tiger')
引用其他模式中的所有表。
如何避免这种行为?我只是想添加一张桌子
在 env.py 中,您可以配置要自动生成的模式/表/列,以及不使用 include_name 和 include_object 函数生成的模式/表/列。 链接到文档 https://alembic.sqlalchemy.org/en/latest/autogenerate.html#autogenerate-include-hooks
我只是过滤了 target_metadata 中不存在的所有表:
def include_name(name, type_, parent_names):
if type_ == "table":
return name in target_metadata.tables
return True
...
context.configure(
# ...
include_name=include_name
)