Alembic generate unnecessary alter commands to column for Identity #1181
-
Hi Recently we added dialect_compat for our application DB referring this for postgresDB, so when application is setup automatically all existing ORM models would be leveraging Identity for autoincrement feature. from sqlalchemy.schema import CreateColumn
from sqlalchemy.ext.compiler import compiles
@compiles(CreateColumn, 'postgresql')
def use_identity(element, compiler, **kw):
text = compiler.visit_create_column(element, **kw)
text = text.replace(
"BIGSERIAL NOT NULL", " BIGINT GENERATED BY DEFAULT AS IDENTITY"
)
return text after adding this forsome reason alembic is creating a revision file for me where it is trying to remove Identity in upgrade method i assume it is because my ORM model column does not have Identity defined in Column()? def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('article', 'id',
existing_type=sa.BIGINT(),
server_default=None,
existing_nullable=False,
autoincrement=True)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('article', 'id',
existing_type=sa.BIGINT(),
server_default=sa.Identity(always=False, start=1, increment=1, minvalue=1, maxvalue=9223372036854775807, cycle=False, cache=1),
existing_nullable=False,
autoincrement=True)
# ### end Alembic commands ### Please refer to MRC for same, dummy_file.py from sqlalchemy.schema import CreateColumn
from sqlalchemy.ext.compiler import compiles
@compiles(CreateColumn, 'postgresql')
def use_identity(element, compiler, **kw):
text = compiler.visit_create_column(element, **kw)
text = text.replace(
"BIGSERIAL NOT NULL", " BIGINT GENERATED BY DEFAULT AS IDENTITY"
)
return text
from sqlalchemy import Column, BigInteger, Unicode, UnicodeText, create_engine, Identity
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import create_session
Base = declarative_base()
class Article(Base):
__tablename__ = 'article'
id = Column(BigInteger, primary_key=True, autoincrement=True)
name = Column(Unicode(255))
content = Column(UnicodeText)
engine = create_engine('postgresql://postgres:postgres@localhost:5432/test', echo=True)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine) # Just to reproduce error assume my older migration files are generating these ORM Models if i do But if i Do I assumed alembic might be looking in Column attributes somewhere in package and saw that Identity is not specified but DB has Identity based table specified due to Is there a way where i don't have to update all Column method with Identity for my ORM models for Alembic to not upgrade Models? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hi,
that's correct. use Identity in the orm model columns or setup an appropriate filter to atogenerate https://alembic.sqlalchemy.org/en/latest/autogenerate.html#controlling-what-to-be-autogenerated
alembic has no way of knowing that you are modifying the create table sql to add identity. All it sees is that the table in the metadata has no identity, while the db has it, so it renders the removal of it. |
Beta Was this translation helpful? Give feedback.
Hi,
that's correct. use Identity in the orm model columns or setup an appropriate filter to atogenerate https://alembic.sqlalchemy.org/en/latest/autogenerate.html#controlling-what-to-be-autogenerated
alembic has no way of knowing that you are modifying the create table sql to add identity. All it sees is that the table in the metadata has no identity, while the db has it, so it renders the removal of it.