Open deajan opened 2 years ago
To guess, when used as a table object, there is no verification process because it is used to map data that has already been verified in database.
(I think because SQLModel
use DeclarativeMeta
when table=True. link)
In particular, when calling many objects from database, there is no unnecessary verification process, so there is a performance advantage.
(I haven't checked yet.)
Of course, if the verification process is essential, SQLModel.validate
method can be used.
I've asked the same question before, but I'm just using SQLModel.validate
method for now. I want to know why, too.
I do understand that too, reads from database don't need validation. But writes do. Nevertheless it's not really clear in the documentation. And since SQLModel is supposed to reduce code duplication, having a class for ORM and a class for Pydantic validation.... doesn't help deduplicate.
@deajan I don't know the answer to why, but I found a simple way to solve it. link
I think this is answered here: https://github.com/tiangolo/sqlmodel/issues/52#issuecomment-1311987732
For future reference, I found this while looking for why model_validate_json
did not properly validate for a SQLModel
with table=True
.
This feature/bug should be fully documented, as I (and many others) expect the SQL model to validate data. Only after doing a lot of digging to find out why one of my field_validator wasn't working did I come across this GitHub issue.
I agree - this just totally threw me off. I expected it to be fully validating my table data. why bother inheriting from BaseModel at all if not.
This feature/bug should be fully documented, as I (and many others) expect the SQL model to validate data. Only after doing a lot of digging to find out why one of my field_validator wasn't working did I come across this GitHub issue.
completely annoying bug. I've lost half a day trying to understand what was happening. I'll reconsider if SQLModel is actually the way to go. Thank you, guys!
This was also very surprising to me. Looks like this PR may fix the issue, although it's unclear if it's the right approach.
First Check
Commit to Help
Example Code
Description
When creating a SQLModel class instance, I thought that I'd have all the advantages of pydantic and SQLAlchemy classes toghether. I was quite surprised when I realized that SQLModel classes created with
table=True
do not validate data as a pydantic class would do (see my example code). Having read about all SQLModel documentation, I didn't find anywhere this is stated. Especially in https://sqlmodel.tiangolo.com/tutorial/fastapi/multiple-models/ it would make sense to explain that atable=True
won't do data validation as a pydantic class would do.Maybe I did not understand the purpose of SQLModel enough, but this is quite disturbing for me. I think it would be really nice to update the documentation regarding this particular point.
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.8
Python Version
Python 3.10.7 x64
Additional Context
PS: Sorry, I'm comming here from Tortoise-ORM, and it's my first time using SQLModel, so I cannot guarantee that I will be quite helpful resolving issues here yet ;) I'll do my best.