SQLAlchemy + alembic:创建模式迁移

时间:2018-03-30 22:06:47

标签: sqlalchemy alembic flask-migrate

我不确定如何定义create schema foo迁移?我的模型看起来像这样(我使用Flask-Migrate):

class MyTable(db.Model):
    __tablename__ = "my_table"
    __table_args__ = {"schema": "foo"}

    id = ColumnPrimKey()
    name = Column(Text, unique=True, nullable=False)

当我执行mange db upgrade时,我得到了一个失败,因为架构" foo"不存在。如何使用SQLAlchemy和Alembic为架构添加迁移?

1 个答案:

答案 0 :(得分:6)

我通过将迁移upgrade命令修改为首次运行来完成此操作:

op.execute("create schema foo")

downgrade函数

op.execute("drop schema foo")

所以整个迁移文件看起来像:

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6c82c972c61e'
down_revision = '553260b3e828'
branch_labels = None
depends_on = None


def upgrade():
    op.execute("create schema foo")
    ...

def downgrade():
    ...
    op.execute("drop schema foo")
相关问题