Skip to content

Inconsistent UUID parsing behaviors between SQLModel (with table=True) and plain SQLModel/Pydantic BaseModel #925

Discussion options

You must be logged in to vote

Ohh, I get what you are saying. Yes validation is not active for table=True models. Check that thread: #52

But what you can do is validate yourself like that:

from sqlmodel import SQLModel, Field
from pydantic import BaseModel

from uuid import UUID

class STableModel(SQLModel, table=True):
    id: UUID = Field(primary_key=True)

uuid: str = "8fd679f6-db1a-4181-b8f4-d4671e4e600b"

s = STableModel.model_validate(STableModel(id=uuid))
print("STableModel", s)

Or use the sqlalchemy validates annotation to be sure the string passed is a valid UUID before instantiating.

from sqlmodel import SQLModel, Field
from sqlalchemy.orm import validates
from uuid import UUID

class STableModel(SQLModel, t…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@tokikanno
Comment options

@joachimhuet
Comment options

Answer selected by tokikanno
@tokikanno
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants