Skip to main content

SQLAlchemy column types for the whenever datetime library

Project description

whenever-sqlalchemy

SQLAlchemy column types for the whenever datetime library.

Installation

pip install whenever-sqlalchemy

Type mapping

whenever type SQL type
Instant TIMESTAMP WITH TIME ZONE
PlainDateTime TIMESTAMP (no timezone)
Date DATE
Time TIME

Usage

SQLAlchemy 2.x — Mapped[] style (recommended)

Register type_annotation_map on your DeclarativeBase once, then use Mapped[Instant] exactly like you would use Mapped[datetime]:

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from whenever import Date, Instant, PlainDateTime, Time
from whenever_sqlalchemy import type_annotation_map

class Base(DeclarativeBase):
    type_annotation_map = type_annotation_map  # merge with your own if needed

class Event(Base):
    __tablename__ = "events"
    id: Mapped[int] = mapped_column(primary_key=True)
    timestamp: Mapped[Instant]
    scheduled: Mapped[PlainDateTime | None]
    date: Mapped[Date | None]
    time: Mapped[Time | None]

SQLAlchemy 1.4 / 2.x — Column() style

from sqlalchemy import Column, Integer
from whenever_sqlalchemy import DateType, InstantType, PlainDateTimeType, TimeType

class Event(Base):
    __tablename__ = "events"
    id = Column(Integer, primary_key=True)
    timestamp = Column(InstantType)
    scheduled = Column(PlainDateTimeType, nullable=True)
    date = Column(DateType, nullable=True)
    time = Column(TimeType, nullable=True)

Notes

Nanosecondswhenever types support nanosecond precision; SQL TIMESTAMP columns store at most microsecond precision. Values are truncated (not rounded) to microseconds on write, matching to_stdlib().

server_defaultserver_default=func.now() works as expected: the server sets the value and SQLAlchemy fetches it back, converting it to the appropriate whenever type.

Dialect notes

  • PostgreSQL: uses TIMESTAMPTZ natively; full roundtrip fidelity.
  • SQLite: stores datetimes as text and drops timezone info. InstantType reattaches UTC tzinfo on retrieval (the stored value is UTC).
  • MySQL / MariaDB / SQL Server: similar to SQLite for timezone columns; the UTC-reattach workaround covers these too.

Compatibility

  • Python 3.10+
  • whenever 0.10+
  • SQLAlchemy 1.4+ (Column() style); SQLAlchemy 2.0+ (Mapped[] style)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

whenever_sqlalchemy-0.1.0b2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

whenever_sqlalchemy-0.1.0b2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file whenever_sqlalchemy-0.1.0b2.tar.gz.

File metadata

  • Download URL: whenever_sqlalchemy-0.1.0b2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for whenever_sqlalchemy-0.1.0b2.tar.gz
Algorithm Hash digest
SHA256 f69b8f8235c41abab24f2d2dd38f9139aa3ccc94b98e5b18a4e919bd1f3acde3
MD5 08f83371cfcd1bda6dea8fbffad81734
BLAKE2b-256 ac06df4dc0f35f7b751d329891a5d85b69628aa8642ae5df985aeb23568fdee5

See more details on using hashes here.

Provenance

The following attestation bundles were made for whenever_sqlalchemy-0.1.0b2.tar.gz:

Publisher: publish.yml on ariebovenberg/whenever-sqlalchemy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file whenever_sqlalchemy-0.1.0b2-py3-none-any.whl.

File metadata

File hashes

Hashes for whenever_sqlalchemy-0.1.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 32afd6c13dc03280dede903edc7e1806475ff715fd2446100846eda22749214e
MD5 b1e18b575992e8cc88750096d7f1baad
BLAKE2b-256 826b16427d0f55be0730215c68075cdb1d204b14b6606149a2407f6d7e42546c

See more details on using hashes here.

Provenance

The following attestation bundles were made for whenever_sqlalchemy-0.1.0b2-py3-none-any.whl:

Publisher: publish.yml on ariebovenberg/whenever-sqlalchemy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page