Skip to content

can't subtract offset-naive and offset-aware datetimes #152

@om-livebird

Description

@om-livebird
from sqlalchemy import Column, String, DateTime, ForeignKey, Enum, Integer
from sqlalchemy.orm import relationship
from sqlalchemy_history import make_versioned, versioning_manager
from app.db import Base, SoftDeleteMixin
from app.util_functions import get_utc_now
from app.v1.computed_attribute.dtos.computed_attribute_enum import OutputDataType

make_versioned(user_cls=None)


class ComputedAttribute(Base, SoftDeleteMixin):
    __tablename__ = "computed_attribute"
    __versioned__ = {}

    name = Column(String, nullable=False)
    description = Column(String, nullable=True)
    expression = Column(String, nullable=False)
    output_data_type = Column(Enum(OutputDataType), nullable=False)
    execution_index = Column(Integer, nullable=False, default=0, server_default="0")

    entity_id = Column(Integer, ForeignKey("entity.id"), nullable=True)
    group_id = Column(Integer, ForeignKey("entity_group.id"), nullable=True)

    created_at = Column(DateTime(timezone=True), nullable=False, default=get_utc_now)
    updated_at = Column(
        DateTime(timezone=True),
        nullable=False,
        default=get_utc_now,
        onupdate=get_utc_now,
    )

    entity = relationship("Entity")
    group = relationship("EntityGroup")

I have this table, in this I added versioning, After I generated the alembic migration which looks like this

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "computed_attribute_version",
        sa.Column("name", sa.String(), autoincrement=False, nullable=True),
        sa.Column("description", sa.String(), autoincrement=False, nullable=True),
        sa.Column("expression", sa.String(), autoincrement=False, nullable=True),
        sa.Column(
            "output_data_type",
            ENUM(
                "NUMBER", "BOOLEAN", "STRING", name="outputdatatype", create_type=False
            ),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column("execution_index", sa.Integer(), autoincrement=False, nullable=True),
        sa.Column("entity_id", sa.Integer(), autoincrement=False, nullable=True),
        sa.Column("group_id", sa.Integer(), autoincrement=False, nullable=True),
        sa.Column(
            "created_at", sa.DateTime(timezone=True), autoincrement=False, nullable=True
        ),
        sa.Column(
            "updated_at", sa.DateTime(timezone=True), autoincrement=False, nullable=True
        ),
        sa.Column("id", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("is_deleted", sa.Boolean(), autoincrement=False, nullable=True),
        sa.Column(
            "transaction_id", sa.BigInteger(), autoincrement=False, nullable=False
        ),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.PrimaryKeyConstraint("id", "transaction_id"),
    )
    op.create_index(
        op.f("ix_computed_attribute_version_end_transaction_id"),
        "computed_attribute_version",
        ["end_transaction_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_computed_attribute_version_operation_type"),
        "computed_attribute_version",
        ["operation_type"],
        unique=False,
    )
    op.create_index(
        op.f("ix_computed_attribute_version_transaction_id"),
        "computed_attribute_version",
        ["transaction_id"],
        unique=False,
    )
    op.create_table(
        "transaction",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("remote_addr", sa.String(length=50), nullable=True),
        sa.Column("issued_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("transaction")
    op.drop_index(
        op.f("ix_computed_attribute_version_transaction_id"),
        table_name="computed_attribute_version",
    )
    op.drop_index(
        op.f("ix_computed_attribute_version_operation_type"),
        table_name="computed_attribute_version",
    )
    op.drop_index(
        op.f("ix_computed_attribute_version_end_transaction_id"),
        table_name="computed_attribute_version",
    )
    op.drop_table("computed_attribute_version")
    # ### end Alembic commands ###

But now when I add a record in DB, I get the error

[SQL: INSERT INTO transaction (id, remote_addr, issued_at) VALUES (nextval('transaction_id_seq'), $1::VARCHAR, $2::TIMESTAMP WITHOUT TIME ZONE) RETURNING transaction.id]
[parameters: (None, datetime.datetime(2025, 12, 8, 11, 44, 22, 254552, tzinfo=datetime.timezone.utc))]
(Background on this error at: https://sqlalche.me/e/20/dbapi)

What wrong am I doing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions