Skip to content

Autogenerated migration misses changes to server_default and onupdate #1203

Description

@jankatins

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:

metadata_schema_name = "test_schema"
Base: Any = declarative_base(metadata=MetaData(schema=metadata_schema_name))
class Test(Base):
    __tablename__ = "_test"
    id = Column(BigInteger, primary_key=True, nullable=False)
    created = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
    last_updated = Column(DateTime(timezone=True), onupdate=func.now())

Alembic generates a migration for this:

    op.create_table('_test',
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('created', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('last_updated', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='test_schema'
    )

Changing the model to:

class Test(Base):
    __tablename__ = "_test"
    id = Column(BigInteger, primary_key=True, nullable=False)
    created = Column(DateTime(timezone=True), server_default=func.clock_timestamp(), nullable=False)
    last_updated = Column(DateTime(timezone=True), server_default=func.clock_timestamp(),
                          onupdate=func.clock_timestamp(), )

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:

    op.create_table('_test2',
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('created', sa.DateTime(timezone=True), server_default=sa.text('clock_timestamp()'), nullable=False),
    sa.Column('last_updated', sa.DateTime(timezone=True), server_default=sa.text('clock_timestamp()'), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='test_schema'
    )
    op.drop_table('_test', schema='test_schema')

(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.

  • OS: Mac, latest version, intel
  • Python: 3.11
  • Alembic: alembic==1.9.4, alembic-utils==0.8.1
  • SQLAlchemy: SQLAlchemy==2.0.4
  • Database: PG 15
  • DBAPI: psycopg2-binary==2.9.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions