You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This one just declares that the id field is required when reading a hero from the API, because a hero read from the API will come from the database, and in the database it will always have an ID.
class HeroBase(SQLModel):
name: str = Field(index=True)
secret_name: str
age: Optional[int] = Field(default=None, index=True)
class Hero(HeroBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class HeroCreate(HeroBase):
pass
class HeroRead(HeroBase):
id: int
Actually trying this gets you:
NameError: Field name "id" shadows a BaseModel attribute; use a different field name with "alias='id'".
When trying to use HeroRead as a response schema for a fastapi endpoint.
Operating System
Linux
Operating System Details
Docker base on python:3.12
SQLModel Version
sqlmodel==0.0.11
pydantic is at 1.10.13, if it matters. I don't specify this, it is auto resolved for sqlmodel and fastapi.
Python Version
3.12.0
Additional Context
Posting this here instead of fastapi or pydantic because this seems like an intentional feature of those two, but conflicts with the aforementioned documentation.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
From Multiple Models
Actually trying this gets you:
When trying to use
HeroRead
as a response schema for a fastapi endpoint.Operating System
Linux
Operating System Details
Docker base on
python:3.12
SQLModel Version
sqlmodel==0.0.11
pydantic is at 1.10.13, if it matters. I don't specify this, it is auto resolved for sqlmodel and fastapi.
Python Version
3.12.0
Additional Context
Posting this here instead of fastapi or pydantic because this seems like an intentional feature of those two, but conflicts with the aforementioned documentation.
Beta Was this translation helpful? Give feedback.
All reactions