SQL Server: Possible to tell Alembic to ignore sa.Identity(always=True)? #1533
-
I know that SQL Server does NOT support The following script creates the # pip install SQLAlchemy alembic pyodbc
from sqlalchemy import (
URL,
Identity,
create_engine,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class ModelBase(DeclarativeBase):
pass
class Model(ModelBase):
__tablename__ = "model"
id: Mapped[int] = mapped_column(
Identity(always=True, start=1, increment=1),
primary_key=True,
)
engine = create_engine(
URL.create(
"mssql+pyodbc",
username="<redacted>",
password="<redacted>",
host="127.0.0.1",
port=1433,
database="<redacted>",
query={
"driver": "ODBC Driver 17 for SQL Server",
"TrustServerCertificate": "yes",
},
)
)
ModelBase.metadata.create_all(engine) Then I installed Alembic and setup autogenerate. After I ran def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('model', 'id',
existing_type=sa.INTEGER(),
server_default=sa.Identity(always=True, start=1, increment=1),
existing_nullable=False,
autoincrement=True)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('model', 'id',
existing_type=sa.INTEGER(),
server_default=sa.Identity(always=False, start=1, increment=1),
existing_nullable=False,
autoincrement=True)
# ### end Alembic commands ### (Basically it trys to set If I run
Is it possible to tell Alembic to ignore |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hi, You can control what alembic renders when using autogenerate with https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.configure.params.include_object |
Beta Was this translation helpful? Give feedback.
-
I added the following to import os
def include_object(
object,
name,
type_,
reflected,
compare_to,
):
if os.getenv("sqlalchemy_connection_string","").startswith("mssql") and type_ == "column":
if object.info.get("alembic_skip_flag", "") == "sql_server":
return False
return True Then I added This solution is good enough for me, though I think it would omit any and all changes to the primary key column. |
Beta Was this translation helpful? Give feedback.
-
For future readers, you can use this import os
from sqlalchemy.sql.schema import SchemaItem
import typing as ty
def include_object(
# https://github.com/sqlalchemy/alembic/blob/main/alembic/runtime/environment.py#L66
object: SchemaItem,
name: ty.Optional[str],
type_: ty.Literal[
"schema",
"table",
"column",
"index",
"unique_constraint",
"foreign_key_constraint",
],
reflected: bool,
compare_to: ty.Optional[SchemaItem],
) -> bool:
if (
# Check database is SQL Server
os.getenv("sqlalchemy_connection_string", "").startswith("mssql")
and type_ == "column"
):
# If database is SQL Server AND column is Identity(always=True),
# prevent Alembic from generating migration for identity.always
if object.identity and object.identity.always is True:
object.identity.always = False
return True |
Beta Was this translation helpful? Give feedback.
I'm suggesting something like
I haven't tried it, but this should make the identity compare equally