使用不同的 postgres 架构进行 Flask 迁移 ( __table_args__ = {'schema': 'test_schema']})

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

我正在尝试使用flask、sqlalchemy 和flask_migrate...

但是每次运行

manage.py migrate
,alembic 总是将我的模型检测为新表。

我认为我将

__table_args__
放入模型中以将表存储在不同的 postgres 模式中:

class Entry(db.Model):
    __table_args__ = {'schema': app.config['BASE_SCH']}
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    slug = db.Column(db.String(100), unique=True)
    body = db.Column(db.Text)
    status = db.Column(db.SmallInteger, default=STATUS_PUBLIC)
    created_timestamp = db.Column(db.DateTime, default=datetime.datetime.now)
    modified_timestamp = db.Column(db.DateTime, default=datetime.datetime.now,
                                   onupdate=datetime.datetime.now)

如果我删除模型的

__table_args__
行,烧瓶迁移将正常工作。将我的表存储在公共 postgres 架构中。

那么,如何在 Flask 中使用不同的 postgres 表模式?

flask flask-sqlalchemy alembic flask-migrate
1个回答
8
投票

终于弄清楚了:配置中有一个

include_schemas
选项,您必须将其设置为
True
以强制 Alembic 在生成迁移之前扫描所有模式。

(稍微)更多详细信息:http://alembic.zzzcomputing.com/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.configure.params.include_schemas

我并不完全清楚为什么 Alembic/Flask-Migrate 会为非默认模式中的表生成迁移没有首先设置此选项......或者更确切地说,它会为非默认模式创建迁移但在数据库中没有发现这些表是一个令人惊讶的行为。

© www.soinside.com 2019 - 2024. All rights reserved.