SQLAlchemy + Alembic:创建架构迁移

s5s*_*s5s 7 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)
Run Code Online (Sandbox Code Playgroud)

执行时,mange db upgrade我失败了,因为模式“ foo”不存在。如何使用SQLAlchemy和Alembic为架构添加迁移?

buc*_*uck 10

我通过修改迁移upgrade命令使其首次运行来实现了这一点:

op.execute("create schema foo")
Run Code Online (Sandbox Code Playgroud)

并且在downgrade功能上

op.execute("drop schema foo")
Run Code Online (Sandbox Code Playgroud)

因此,整个迁移文件如下所示:

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")
Run Code Online (Sandbox Code Playgroud)