Skip to content

Best way to add nested model with relationship in Create #1116

Answered by gzu300
jonyscathe asked this question in Questions
Discussion options

You must be logged in to vote

I ran into the same issue and was inspired by this thread. Here’s my refinement:

I introduced Helper models that sit between the Create schemas and the table=True ORM models. These helpers use @field_validator(..., mode="after") to recursively convert nested data into ORM-compatible objects — without mutating the original input schemas or embedding transformation logic into the ORM models.

This approach keeps input validation clean while making the transformation layer explicit, type-safe, and scalable.

✅ Minimal working example

from sqlmodel import SQLModel, Field, Relationship
from pydantic import field_validator

# --- Input Schemas ---
class AddressCreate(SQLModel):
    city: str

class 

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@jonyscathe
Comment options

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