Flask Migrate using different postgres schemas ( __table_args__ = {'schema': 'test_schema']})

一个人想着一个人 提交于 2019-12-24 02:04:47

问题


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

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

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)

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

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

Thanks!


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/40577640/flask-migrate-using-different-postgres-schemas-table-args-schema-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!