Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on model with server_default for DateTime column and UUIDType primary key. #727

Open
CrossNox opened this issue Feb 17, 2024 · 0 comments

Comments

@CrossNox
Copy link

Environment

  • python==3.11
  • slqalchemy-utils==0.41.1
  • sqlalchemy==2.0.27
  • postgres==16.1

Description

Consider the following models:

import uuid

from sqlalchemy.sql import func
from sqlalchemy_utils import UUIDType
from sqlalchemy.orm import relationship
from sqlalchemy import ARRAY, Enum, Float, Column, String, DateTime, ForeignKey

from sentinel import __version__
from sentinel.models.base import Base, PrintableBase, UpdatableBase


class ObjA(Base, UpdatableBase, PrintableBase):

    __tablename__ = "obj_a"

    id = Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4)
    string_a = Column(String, nullable=False)

    created_at = Column(
        DateTime,
        nullable=False,
        # default=datetime.utcnow,
        server_default=func.now(),  # pylint: disable=not-callable
    )

    objs_a_to_b = relationship("ObjAtoB", back_populates="obj_a")


class ObjAtoB(Base, UpdatableBase, PrintableBase):

    __tablename__ = "objs_a_to_b"

    id = Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4)
    extra = Column(Float, nullable=False)

    b_id = Column(String, nullable=False)

    created_at = Column(
        DateTime,
        nullable=False,
        # default=datetime.utcnow,
        server_default=func.now(),  # pylint: disable=not-callable
    )

    a_id = Column(UUIDType(binary=False), ForeignKey("obj_a.id"), nullable=False)
    obj_a = relationship("ObjA", back_populates="objs_a_to_b")


class ObjB(Base, UpdatableBase, PrintableBase):
    __tablename__ = "obj_b"

    id = Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4)
    string_b = Column(String, nullable=True)

    created_at = Column(
        DateTime,
        nullable=False,
        # default=datetime.utcnow,
        server_default=func.now(),  # pylint: disable=not-callable
    )
    updated_at = Column(
        DateTime,
        nullable=False,
        # default=datetime.utcnow,
        server_default=func.now(),  # pylint: disable=not-callable
    )

When adding new entities like this:

    bs = [ObjB(string_b=randstr()) for x in range(5)]
    db.add_all(bs)
    db.commit()

    w = ObjA(
        string_a=randstr(),
        objs_a_to_b=[ObjAtoB(extra=10.0, b_id=str(b.id)) for b in bs],
    )
    db.add(w)
    db.commit()

I get the following error:

sentinel-api-1               | sqlalchemy.exc.InvalidRequestError: Can't match sentinel values in result set to parameter sets; key '5bab0055-1d7c-4000-92e8-1892de25bc26' was not found. There may be a mismatch between the datatype passed to the DBAPI driver vs. that which it returns in a result row.  Ensure the given Python value matches the expected result type *exactly*, taking care to not rely upon implicit conversions which may occur such as when using strings in place of UUID or integer values, etc.sentinel-api-1               | sqlalchemy.exc.InvalidRequestError: Can't match sentinel values in result set to parameter sets; key '5bab0055-1d7c-4000-92e8-1892de25bc26' was not found. There may be a mismatch between the datatype passed to the DBAPI driver vs. that which it returns in a result row.  Ensure the given Python value matches the expected result type *exactly*, taking care to not rely upon implicit conversions which may occur such as when using strings in place of UUID or integer values, etc.

You can check that by running docker compose up on this fastapi project.

It works when:

  • Using Uuid from sqlalchemy instead of UUIDType from sqlalchemy-utils
  • Commenting server_default and uncommenting default for datetimes
  • Using sqlalchemy==1.4.51

This issue seems to be related.

Could this be an error from this library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant