使用不同的postgres模式迁移烧瓶(__table_args__ = {'schema':'test_schema']}) [英] Flask Migrate using different postgres schemas ( __table_args__ = {'schema': 'test_schema']})

查看:338
本文介绍了使用不同的postgres模式迁移烧瓶(__table_args__ = {'schema':'test_schema']})的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use flask, sqlalchemy, and flask_migrate...

但是每次运行manage.py迁移时,Alembic始终会将我的模型检测为新表.

But every time run manage.py migrate, alembic always detect my model as a new table.

我认为我在模型中放入了 table_args ,以将表存储在不同的postgres模式中:

I think that i put table_args in my model to store table in a different postgres schema:

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模式中.

If I to delete the table_args line of my model, the flask migrate works properly. Storing my table in puclic postgres schema.

那么,如何在flask中使用不同的postgres表架构?

So, how can I use different postgres table schemas with flask?

谢谢!

推荐答案

最后弄清楚了这一点:configure中有一个include_schemas选项,您必须将其设置为True,以强制Alembic在生成该模式之前扫描所有模式.迁移.

Finally figured this out: there is an include_schemas option in configure that you have to set to True to force Alembic to scan all schemas before generating the migration.

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

(Slightly) more details: http://alembic.zzzcomputing.com/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.configure.params.include_schemas

对于我来说,尚不完全清楚为什么Alembic/Flask-Migrate会为非默认模式中没有的表生成表迁移……或者,实际上是这样设置的会为非默认模式创建迁移,但不会在数据库中发现这些表是令人惊讶的行为.

It's not completely clear to me why Alembic/Flask-Migrate was generating the migrations for tables in non default schemas without this option set in the first place... or rather, the fact that it would create migrations for non default schemas but not discover these tables in the DB is a surprising behavior.

这篇关于使用不同的postgres模式迁移烧瓶(__table_args__ = {'schema':'test_schema']})的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆