Repository navigation
model_validate ignores relationship lists
#1430
Replies: 1 comment
|
For nested request data, validate a non-table input model first, then construct the related table objects explicitly. Using your class ChildInput(SQLModel):
number: int
class ParentInput(SQLModel):
name: str
children: list[ChildInput]
data = ParentInput.model_validate(
{"name": "Hello", "children": [{"number": "4"}]}
)
parent = SqlParent.model_validate(data.model_dump(exclude={"children"}))
parent.children = [SqlChild(number=child.number) for child in data.children]
assert parent.children[0].number == 4
with Session(engine) as session:
session.add(parent)
session.commit()Here Also, printing the parent is not a reliable check: its repr omits relationships even when Checked with Codex assistance on SQLModel 0.0.24/Pydantic 2.11.7 and SQLModel 0.0.48/Pydantic 2.14.1. On both, I committed and reloaded the graph in a new SQLite session and checked the child foreign keys. Invalid nested numbers raise validation errors before any ORM objects are created. |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
Description
Pydantic unpacks lists passed to
model_validatebut sqlmodel does not. If the library claims to be compatible with pydantic I'm not sure if this behaviour is correct or notOperating System
Linux
Operating System Details
No response
SQLModel Version
0.0.24
Python Version
3.13.2
Additional Context
May be related to #1092
All reactions