-
First Check
Commit to Help
Example Codefrom typing import Optional
from pydantic import BaseModel
from sqlalchemy import JSON
from sqlmodel import SQLModel, Field
class Gadget(BaseModel):
description: str = Field(description="Description of the gadget")
class NormalWidget(BaseModel):
id: str = Field(description="Widget ID")
gadget: Optional[Gadget] = Field(description="Gadget associated with the widget")
class SqlModelWidget(SQLModel, table=True):
__tablename__ = "widget"
id: str = Field(default=None, primary_key=True)
gadget: Optional[Gadget] = Field(default=None, sa_type=JSON)
JSON_DATA = """
{
"id": "1",
"gadget": {
"description": "A thingy that does stuff"
}
}
"""
def test_deserialize_data_with_normal_model():
widget = NormalWidget.model_validate_json(JSON_DATA)
assert isinstance(widget.gadget, Gadget), "widget.gadget should be of type Gadget"
def test_deserialize_data_with_sql_model():
widget = SqlModelWidget.model_validate_json(JSON_DATA)
assert isinstance(widget.gadget, Gadget), "widget.gadget should be of type Gadget" DescriptionIn the provided test case, you should see that the normal pydantic model de-serializes the JSON data with the correct type. The Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.22 Python Version3.11.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
mcantrell
Feb 13, 2025
Replies: 1 comment
-
While trying to fix the problem with a custom class SqlModelWidget(SQLModel, table=True):
__tablename__ = "widget"
id: str = Field(default=None, primary_key=True)
gadget: Optional[Gadget] = Field(default=None, sa_type=JSON)
class Config:
validate_assignment = True Updated test passes: import json
from typing import Optional, Any
from pydantic import BaseModel, field_validator
from sqlalchemy import JSON
from sqlmodel import SQLModel, Field
from finra.utils import finra_logger
class Gadget(BaseModel):
description: str = Field(description="Description of the gadget")
class NormalWidget(BaseModel):
id: str = Field(description="Widget ID")
gadget: Optional[Gadget] = Field(description="Gadget associated with the widget")
class SqlModelWidget(SQLModel, table=True):
__tablename__ = "widget"
id: str = Field(default=None, primary_key=True)
gadget: Optional[Gadget] = Field(default=None, sa_type=JSON)
class Config:
validate_assignment = True
JSON_DATA = """
{
"id": "1",
"gadget": {
"description": "A thingy that does stuff"
}
}
"""
def test_deserialize_data_with_normal_model():
widget = NormalWidget.model_validate_json(JSON_DATA)
assert isinstance(widget.gadget, Gadget), "widget.gadget should be of type Gadget"
def test_deserialize_data_with_sql_model():
widget = SqlModelWidget.model_validate_json(JSON_DATA)
assert isinstance(widget.gadget, Gadget), "widget.gadget should be of type Gadget" |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mcantrell
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While trying to fix the problem with a custom
field_validator
, I noticed that my validator wasn't being called. I found #52 (comment) while trying to research relatedfield_validator
questions. It seems that adding thevalidate_assignment
config fixes the issue.Updated test passes: