Skip to content

SQLModel + Pydantic v1 vs SQLModel + Pydantic v2 - different assignment behaviour #824

Answered by YuriiMotov
ta-soft asked this question in Questions
Discussion options

You must be logged in to vote

SQLModels with table=True bypass validation, you shouldn't use models with table=True for input validation.

Create ABase model, move common fields there and use it for validation:

from sqlmodel import SQLModel, Field
from datetime import date
from typing import Optional


class ABase(SQLModel):
    id: Optional[int] = Field(default=None, primary_key=True)
    dob: date


class A(ABase, table=True):
    pass


a = ABase(dob="1980-01-01")
print(a)  # id=None dob=datetime.date(1980, 1, 1)

Replies: 4 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
@ta-soft
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
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