11月05, 2020

Flask Migrate using different postgres schemas

Flask Sqlalchemy Migration可以方便的生成数据库升级脚本,当需要将表分配到不同的pg schema时,默认在生成升级脚本时是不会扫描public schema外的表.

指定schema

table_args = {'schema': app.config['BASE_SCH']}

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)

更新include_schemas配置

在migrations目录下的env.py文件,增加一行include_schemas=True,

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=process_revision_directives,
            include_schemas=True,
            **current_app.extensions['migrate'].configure_args
        )

参考文献

https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.configure.params.include_schemas

本文链接:http://57km.cc/post/Flask Migrate using different postgres schemas.html

-- EOF --

Comments