鉴于此模型:
from typing import Optional
from sqlmodel import SQLModel, Field
class SongBase(SQLModel):
name: str
artist: str = Field(index=False)
#label: Optional[str] = Field(None, index=False)
year: Optional[int] = Field(None, index=False)
class Song(SongBase, table=True):
id: int = Field(default=None, primary_key=True, index=False)
class SongCreate(SongBase):
pass
我使用创建初始 alembic 修订alembic revision --autogenerate -m "init"
,然后使用alembic upgrade head
.
现在我取消注释该label
字段,然后运行alembic revision --autogenerate -m "label"
. 我的迁移显示如下:
revision = '083a8e84f047'
down_revision = 'c1b2ad7d0a39'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('song', sa.Column('label', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
op.alter_column('song', 'id',
existing_type=sa.INTEGER(),
nullable=True,
autoincrement=True)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('song', 'id',
existing_type=sa.INTEGER(),
nullable=False,
autoincrement=True)
op.drop_column('song', 'label')
# ### end Alembic commands ###
为什么 alembic 试图更改 id 字段?我们正在尝试评估 sqlmodel/alembic 以查看它对于生产工作负载是否可行,并且必须手动进行迁移以摆脱这些主键操作对我来说似乎有点危险。我做错了什么让alembic想以这种方式编辑我的主键字段吗?
编辑:为了披露,模型来自这篇文章/示例:https ://github.com/testdrivenio/fastapi-sqlmodel-alembic