sqlalchemy、alembic 与 postgres 和 postgis 在其他模式中删除表

问题描述 投票:0回答:1

我正在使用 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')
引用其他模式中的所有表。

如何避免这种行为?我只是想添加一张桌子

python sqlalchemy alembic
1个回答
0
投票

在 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
)
© www.soinside.com 2019 - 2024. All rights reserved.