gpt4 book ai didi

sqlalchemy - SQLAlchemy + Alembic:创建架构迁移

转载 作者:行者123 更新时间:2023-12-04 06:14:53 30 4
gpt4 key购买 nike

我不确定如何定义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为架构添加迁移?

最佳答案

我通过修改migration 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")

关于sqlalchemy - SQLAlchemy + Alembic:创建架构迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49582020/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com