I am trying to use alembic in my FastAPI/SQLAlchemy app I have a strange code in migration (more details below)
INFO [alembic.autogenerate.compare] Detected added unique constraint None on '('id',)'
Why do I have this line in migration?
All actions:
Created and applied initial migration (looks good)
alembic revision --autogenerate -m "InitialMigration"
---Connecting to PostgreSQL---
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'tasks'
INFO [alembic.autogenerate.compare] Detected added table 'solutions'
INFO [alembic.autogenerate.compare] Detected added table 'tasks_files'
Generating /usr/src/app/app/alembic/versions/05f3f7393aa7_initialmigration.py ... done
alembic upgrade head
---Connecting to PostgreSQL---
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 05f3f7393aa7, InitialMigration
Migration file looks valid
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('tasks',
sa.Column('id', sa.String(), nullable=False),
sa.Column('external_id', sa.String(), nullable=False),
...
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('id')
)
Then I added new_field
column to tasks
table
And migration looks strange
alembic revision --autogenerate -m "AddNewField"
---Connecting to PostgreSQL---
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.autogenerate.compare] Detected added unique constraint None on '('id',)'
INFO [alembic.autogenerate.compare] Detected added column 'tasks.new_field'
INFO [alembic.autogenerate.compare] Detected added unique constraint None on '('id',)'
INFO [alembic.autogenerate.compare] Detected added unique constraint None on '('id',)'
Migration file
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint(None, 'solutions', ['id'])
op.add_column('tasks', sa.Column('new_field', sa.String(), nullable=True))
op.create_unique_constraint(None, 'tasks', ['id'])
op.create_unique_constraint(None, 'tasks_files', ['id'])
# ### end Alembic commands ###
My model:
class Task(Base):
__tablename__ = 'tasks'
id = Column(String, unique=True, nullable=False, primary_key=True)
external_id = Column(String, nullable=False)
...
new_field = Column(String)
Removed unique=True
from
id = Column(String, unique=True, nullable=False, primary_key=True)
and it solved the problem