TimeStampedModel

class specatalog.models.base.TimeStampedModel(**kwargs)[source]

Abstract base class providing automatic timestamp fields for all inheriting SQLAlchemy models.

This class adds two metadata fields, created_at and updated_at, which are populated automatically whenever an object is created or modified.

Parameters:

kwargs (Any)

created_at

Timestamp indicating when the record was first created. Automatically assigned at insert time using datetime.now(datetime.UTC).

Type:

datetime.datetime

updated_at

Timestamp indicating when the record was last modified. Automatically updated before any SQL UPDATE operation. May be None if the record has not been changed since creation.

Type:

datetime.datetime or None

Notes

  • The class is declared __abstract__ and therefore does not create its own database table.

  • updated_at is updated using a SQLAlchemy before_update event listener attached to TimeStampedModel with propagate=True.

  • Model.query is set to Session.query_property(), allowing models inheriting from TimeStampedModel to use Model.query in a Flask-style query interface (MyModel.query.filter(...)).

Examples

Creating a new model with timestamps:

>>> class Molecule(TimeStampedModel):
...     __tablename__ = "molecules"
...     id = Column(Integer, primary_key=True)
...     name = Column(String)
>>> m = Molecule(name="Water")
>>> session.add(m)
>>> session.commit()

Accessing timestamps:

>>> m.created_at
datetime.datetime(2025, 4, 12, 14, 30, tzinfo=datetime.UTC)
>>> m.updated_at
None

After modification:

>>> m.name = "Heavy Water"
>>> session.commit()
>>> m.updated_at
datetime.datetime(2025, 4, 12, 14, 32, tzinfo=datetime.UTC)