我不确定如何定义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/