Repository navigation
Replies: 3 comments
|
I investigated this issue and implemented a solution locally. I added support for the extra parameter in the model_validate and the sqlmodel_validate function since the underlying ExtraBehaviour is already implemented in Core Schema. Also created a test to run a verification covering the issue. Before opening a PR, just wanted to check whether this is the intended approach in this matter. |
|
Hey, just looked into this — the issue is straightforward. SQLModel's model_validate in main.py and sqlmodel_validate in _compat.py both accept strict, from_attributes, context, and update, but extra got left out even though @RandhawaP2005 your approach sounds right to me! You'd need to touch two spots: sqlmodel/main.py — add extra to the signature and pass it down: And the same in _compat.py — add extra to sqlmodel_validate and pass it into the validate_python() call. Literal is already imported so no new deps needed. Would love to see a PR on this, seems like a clean fix! |
|
Still true on the latest release. In SQLModel 0.0.50 Two things I found while checking this (pydantic 2.14.4):
Until that lands, set it in the model config. That works on both table and non-table models. The usual SQLModel pattern is a separate input model: from pydantic import ConfigDict
from sqlmodel import Field, SQLModel
class HeroBase(SQLModel):
name: str
secret_name: str
age: int | None = None
class HeroCreate(HeroBase):
model_config = ConfigDict(extra="forbid")
class Hero(HeroBase, table=True):
id: int | None = Field(default=None, primary_key=True)
data = {"name": "Deadpond", "secret_name": "Dive Wilson", "another_field": "abc"}
hero = Hero.model_validate(HeroCreate.model_validate(data))
# ValidationError: another_field - Extra inputs are not permitted [type=extra_forbidden]Putting |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
Description
The parameter
extrais supported in the pydanticmodel_validate():https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_validate
But that parameter is not supported in sqlmodel due to that method being overridden:
https://github.com/fastapi/sqlmodel/blob/main/sqlmodel/main.py#L873
https://github.com/fastapi/sqlmodel/blob/main/sqlmodel/_compat.py#L276
The
validate_python()method already takesextra, so this should be a simple fix.I can take a look at a PR if that would be helpful.
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.37
Python Version
Python 3.11.14
Additional Context
No response
All reactions