Primary Key Change Not Detected #1776
-
|
Hi, I'm working on a migration where I need to change the primary key column of a table. I changed the model accordingly but the change is not detected by alembic. Specs:
Sample model: from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy import MetaData
class Base(DeclarativeBase):
metadata = MetaData(
naming_convention={
"ix": "IX_%(column_0_label)s",
"uq": "UQ_%(table_name)s_%(column_0_name)s",
"ck": "CK_%(table_name)s_%(constraint_name)s",
"fk": "FK_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "PK_%(table_name)s",
},
)
class Parent(Base):
__tablename__ = "parent"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(unique=True)Steps to reproduce:
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('parent',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('PK_parent')),
sa.UniqueConstraint('name', name=op.f('UQ_parent_name'))
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('parent')
# ### end Alembic commands ###
class Parent(Base):
__tablename__ = "parent"
id: Mapped[int] = mapped_column()
name: Mapped[str] = mapped_column(primary_key=True)
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('parent', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('UQ_parent_name'), type_='unique')
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('parent', schema=None) as batch_op:
batch_op.create_unique_constraint(batch_op.f('UQ_parent_name'), ['name'])
# ### end Alembic commands ###The unique constraint has been dropped accordingly but the primary key is still on the old |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
PK changes are not part of autogenerate right now. There's a layer of impracticality to PK changes which is that the correct migrations to modify the PK of a table can be very complicated as the PK constraint can typically only be dropped and recreated, which is often infeasible as it's the target of foreign key constraints from elsewhere. |
Beta Was this translation helpful? Give feedback.
PK changes are not part of autogenerate right now. There's a layer of impracticality to PK changes which is that the correct migrations to modify the PK of a table can be very complicated as the PK constraint can typically only be dropped and recreated, which is often infeasible as it's the target of foreign key constraints from elsewhere.
see also https://alembic.sqlalchemy.org/en/latest/autogenerate.html#what-does-autogenerate-detect-and-what-does-it-not-detect