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