Flask Migrate 使用不同的 postgres 模式( __table_args__ = {'schema': 'test_schema']})

Rôm*_*aes 4 flask flask-sqlalchemy alembic flask-migrate

我正在尝试使用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)
Run Code Online (Sandbox Code Playgroud)

如果我删除模型的table_args行,烧瓶迁移工作正常。将我的表存储在 puclic postgres 模式中。

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

谢谢!

Jul*_*éon 6

终于弄清楚了:include_schemas配置中有一个选项,您必须将其设置True为强制 Alembic 在生成迁移之前扫描所有模式。

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

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