Skip to content

@observes does not detect changes in columns inherited from a mixin #773

@FeehGb

Description

@FeehGb

Description:

I am encountering an issue when using @observes("dt_alteracao") in a model that inherits dt_alteracao from a TimestampMixin. The observer correctly detects changes when the column is modified directly in the model, but it does not trigger when the change comes from the mixin.

Code to reproduce:

from datetime import datetime
from sqlalchemy import Column, TIMESTAMP, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import observes

Base = declarative_base()

class TimestampMixin:
    __abstract__ = True

    __datetime_func__ = text("CURRENT_TIMESTAMP")

    dt_inclusao = Column("dt_inclusao", TIMESTAMP(timezone=False), server_default=__datetime_func__, nullable=False)
    dt_alteracao = Column(
        "dt_alteracao",
        TIMESTAMP(timezone=False),
        server_default=__datetime_func__,
        onupdate=__datetime_func__,
        nullable=False,
    )

class MyModel(Base, TimestampMixin):
    __tablename__ = "my_model"
    id = Column("id", TIMESTAMP(timezone=False), primary_key=True)

    @observes("dt_alteracao")
    def usuario_inclusao_alteracao_observer(self, value: datetime):
        print(f"Observer triggered: {value}")

Expected Behavior:

The observer should be triggered when dt_alteracao is updated, regardless of whether the column comes from the mixin or is defined directly in the model.

Observed Behavior:

The observer is only triggered when dt_alteracao is changed within the model itself, but not when the change originates from the mixin.

Environment:

  • Python version: 3.12
  • SQLAlchemy version: 2.0.36
  • SQLAlchemy-Utils version: 0.41.2

Is this the expected behavior, or is there a way to ensure that changes to mixin-inherited columns are observed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions