Replies: 5 comments
-
|
Several issues have been reported about inconsistencies between sqlmodel and pydantic: #87, #230, etc. This particular issue is caused by these lines in main.py: The last line is assignment, which is forbidden by |
Beta Was this translation helpful? Give feedback.
-
|
I also hope the immutatable model can be possible in sqlmodel. The same problem is reported in stackoverflow here The problem still exists in 0.0.8 |
Beta Was this translation helpful? Give feedback.
-
|
Hi! Does the recent version of SQLModel (0.0.14) solves the issue? :) |
Beta Was this translation helpful? Give feedback.
-
|
I am having the same problem at version 0.0.24, and even setting Below is an example stacktrace, where date_of_creation: datetime = Field(
# allow_mutation=True, # This seems meaningless as it is a pydantic v1 argument
default_factory=datetime.now,
schema_extra={"frozen": True} # see https://github.com/fastapi/sqlmodel/issues/262#issuecomment-1060041950
)The stacktrace is as follows: The last callstack at the SQLModel side is here. I wanted to fix it, but it seems so deep down in the callstack, and is nested inside multiple dunder method calls, so I have no idea how to even start fixing that. Also, can we get a |
Beta Was this translation helpful? Give feedback.
-
|
I just tested it with SQLAlchemy 0.0.24, Pydantic 1.10.22 / 2.11.7 and tests pass. Here is adapted test (in order to work with both, Pydantic V1 and V2) (in the details): Detailsimport pytest
import sqlmodel
import pydantic
if pydantic.VERSION.startswith("2."):
class MyPydanticModel(pydantic.BaseModel):
id: str
data: str = pydantic.Field(frozen=True)
model_config = {"validate_assignment": True}
class MySqlmodelModel(sqlmodel.SQLModel):
id: str
data: str = sqlmodel.Field(schema_extra={"frozen": True})
model_config = {"validate_assignment": True}
else:
class MyPydanticModel(pydantic.BaseModel):
id: str
data: str = pydantic.Field(allow_mutation=False)
class Config:
validate_assignment = True
class MySqlmodelModel(sqlmodel.SQLModel):
id: str
data: str = sqlmodel.Field(allow_mutation=False)
class Config:
validate_assignment = True
class TestSuite:
def test_pydantic_can_create(self):
MyPydanticModel(id='abc', data="foo")
def test_pydantic_raises_on_update(self):
my_pyd_mdl = MyPydanticModel(id='abc', data="foo")
with pytest.raises((pydantic.ValidationError, TypeError)):
my_pyd_mdl.data = "bar"
def test_sqlmodel_can_create(self):
MySqlmodelModel(id='abc', data="foo")
def test_sqlmodel_raises_on_update(self):
my_sqlmdl_mdl = MySqlmodelModel(id='abc', data="foo")
with pytest.raises((pydantic.ValidationError, TypeError)):
my_sqlmdl_mdl.data = "bar"Use case of @EkremDincel is a bit different. And it still doesn't work. import sqlmodel
class MySqlmodelModel(sqlmodel.SQLModel, table=True):
id: str = sqlmodel.Field(primary_key=True)
data: str = sqlmodel.Field(schema_extra={"frozen": True})
model_config = {"validate_assignment": True}
class TestSuite:
def test_sqlmodel_can_create(self):
MySqlmodelModel(id="abc", data="foo")
# 1 validation error for MySqlmodelModel
# data
# Field is frozen |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
When setting a
Field(allow_mutation=False)in combination with the requiredvalidate_assignment = Trueconfiguration on aSQLModel, the framework doesn't even allow the field to be set in the initial construction of the model. This is allowed inpydantic. I've attached a test suite that we should expect to pass, but it fails both theSQLModelspecific tests.Operating System
macOS
Operating System Details
Big Sur 11.3.1
SQLModel Version
0.0.6
Python Version
3.9.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions