Replies: 1 comment 3 replies
-
This can be added as a test with: from typing import Optional
from sqlmodel import Field, SQLModel
def test_sqlmodel_table_param_is_consistent():
class Hero(SQLModel):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
class HeroTable(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
class HeroInheritanceTable(Hero, table=True):
pass
hero = Hero(id="3", name="foo", secret_name="secret_name", age="5")
hero_t = HeroTable(id="3", name="foo", secret_name="secret_name", age="5")
hero_it = HeroInheritanceTable(id="3", name="foo", secret_name="secret_name", age="5")
assert hero.model_dump() == hero_t.model_dump()
assert hero.model_dump() == hero_it.model_dump() |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
Here, hero gets it's types coerced so during the rest of the application in which
hero
is scoped, bothid
andage
are integers. Withtable=True
, we observe thathero_t
does not get this type coercion. This is non-obvious, and leads to bugs where one accesses properties of HeroTable objects expecting certain types and getting others.The example is adjusted from actual production code that was using prior versions of pydantic, in which coercion was always done, regardless of
table=True
.I understand that
table=True
disables validations; however, I believe this is both a regression and undocumented behavior.Not having types of the objects one is working with makes sqlmodel only usable for the simplest of crud projects; which is sad as I have used it with great joy until the newer versions (of sqlmodel/pydantic).
Operating System
Linux, Windows, macOS
Operating System Details
No response
SQLModel Version
0.0.16
Python Version
3.10.12
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions