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.0.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.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: whenever_sqlalchemy-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 0f37dde4efd061da089c942e62d73639ede216922b6cb0f0ac76f9d5beb98b7d
MD5 13edefcb186b0cb4460e8768938d1674
BLAKE2b-256 db2dc3c7e6224482d7a3a531f73926b323a76f199ce6b50ce16f4fb123261ae5

See more details on using hashes here.

Provenance

The following attestation bundles were made for whenever_sqlalchemy-0.1.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for whenever_sqlalchemy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d134b30d52365542b15f14f898bb1c324e20afc59ad5ab69ea184ea63e04946
MD5 96c264de241f45789cc99a83482fc278
BLAKE2b-256 fb51d5cc00014556e59aed4eb5889eb58ddf5c216900f8a7b5d7047b7ba9b732

See more details on using hashes here.

Provenance

The following attestation bundles were made for whenever_sqlalchemy-0.1.0-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