Autogenerated migration misses changes to server_default and onupdate #1204
Replies: 2 comments 1 reply
-
|
Hi, That's working as expected. server default are not compared by default. See https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.configure.params.compare_server_default
not sure what you mean by this. onupdate is related to what sqlalchemy will do when issuing an update https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Column.params.onupdate |
Beta Was this translation helpful? Give feedback.
-
|
Same issue here. Installed packages:
The Excerpt of the migration script for Users model description:
sa.Column('updated_at', sa.DateTime(timezone=True),
server_default=sa.text('now()'),
nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True),
server_default=sa.text('now()'),
server_onupdate=sa.text('now()'),
nullable=False),Would you happen to have any other tips to give? Other infos: Users modelfrom datetime import datetime
from sqlalchemy import DateTime, func
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class Users(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
# It doesn't matter if we use both parameters below or just one of them.
# The results are the same: missing the `server_onupdate` on the migration script.
server_onupdate=func.now(),
onupdate=func.now(),
)Full migration script"""Users: create table
Revision ID: f98bb5629b6e
Revises:
Create Date: 2025-11-23 04:10:00.274823
"""
from typing import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "f98bb5629b6e"
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"users",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("users")
# ### end Alembic commands ### |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
If a SQLA model was already created and now the server_default
or on_update changes/gets added, there is no migration created.Expected behavior
I expect some alter column statement getting generated.
To Reproduce
before:
Alembic generates a migration for this:
Changing the model to:
and running
alembic revision --autogenerate -m "test2"will essentially create an empty migration (I have some autogenerated "CREATE SCHEMA IF NOT EXIST ... " in there). If I instead generate a complete new table (by simply changing the name so the old table gets removed and the new one gets created), I get the correct migration:(Update: I also just realized that no trigger is getting created for onupdate :-( Ouupsi, glad that I saw this...)
Error
None :-( Also no warnings relating to this (I've cycles in my tables and also some warnings re "expression-based index" due to a exclusion statement).
Also no warning that onupdate is not working on PG :-(
Versions.
Beta Was this translation helpful? Give feedback.
All reactions