Inconsistent UUID parsing behaviors between SQLModel (with table=True) and plain SQLModel/Pydantic BaseModel #925
-
First Check
Commit to Help
Example Codefrom sqlmodel import SQLModel, Field
from pydantic import BaseModel
from uuid import UUID
class STableModel(SQLModel, table=True):
id: UUID = Field(primary_key=True)
class SModel(SQLModel):
id: UUID
class PModel(BaseModel):
id: UUID
uuid: str = "8fd679f6-db1a-4181-b8f4-d4671e4e600b"
s = STableModel(id=uuid)
print("STableModel", s)
s = SModel(id=uuid)
print("SModel", s)
p = PModel(id=uuid)
print("PModel", p) DescriptionThe execution result of above codes will be
The This inconsistent UUID parsing behavior will also cause insert/update operation failed with below error message, because the SQLModel(with table=True) won't convert plain string into UUID object on creation.
Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.18 Python VersionPython 3.11.6 (main, Nov 29 2023, 03:49:24) [GCC 12.2.0] on linux Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey, what database technology do you use ? Can you give a reproducible example so that I can run your code getting the sqlalchemy exception ? |
Beta Was this translation helpful? Give feedback.
Ohh, I get what you are saying. Yes validation is not active for
table=True
models. Check that thread: #52But what you can do is validate yourself like that:
Or use the sqlalchemy
validates
annotation to be sure the string passed is a valid UUID before instantiating.