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

Uploaded Python 3

File details

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

File metadata

  • Download URL: whenever_sqlalchemy-0.1.0b1.tar.gz
  • Upload date:
  • Size: 6.9 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.0b1.tar.gz
Algorithm Hash digest
SHA256 1a1e1c438546268d66236a3a9c95cf1a1e30047e882ba300c9699459a0feea29
MD5 4260a3eeeaf99db89d4a899935c55e69
BLAKE2b-256 eac65dfd4752b913e003d7bee888203ca0cd0b0c19ea1430b063722cf928b1a8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for whenever_sqlalchemy-0.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 fba0450429456abd9490fd0762cd9657e9147b77909b8b0b6d953449b1eb6e8f
MD5 23e4cb50be919d66f0ac79528e983c87
BLAKE2b-256 e2839df963d01f028ab3768a4e7e1244de5e9a017b8421c53e1b23149c4f8be3

See more details on using hashes here.

Provenance

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