SQL Alchemy maps UUID-primary key to string

114 Views Asked by At

I'm working with SQLModel and I created following base-table and table:

class GUIDModel(PydanticBase):
    """
    Provides a base mixin for tables with GUID as primary key
    """

    guid: Optional[UUID] = Field(
        ...,
        primary_key=True,
        description=DescriptionConstants.GUID,
        sa_column=Column(
            "guid",
            UNIQUEIDENTIFIER,
            nullable=False,
            primary_key=True,
            server_default=text("newsequentialid()"),
        ),
    )

class Project(GUIDModel):
    name: str = Field(max_length=255, description=DescriptionConstants.NAME)

So everything works, but when I try to get a row with SQL Alchemy, it returns the GUID as a string instead of UUID

def test_get_project(self):
    with Session(__get_engine()) as session:
        project: Project = Projects._get_project(session)
        self.assertEqual(type(project.guid), uuid.UUID)

Error:

<class 'uuid.UUID'> != <class 'str'>
Expected :<class 'str'>
Actual   :<class 'uuid.UUID'>
<Click to see difference>
0

There are 0 best solutions below