sqlmodel
sqlmodel copied to clipboard
Field(allow_mutation=False) doesn't have same behavior as pydantic
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
import pytest
import sqlmodel
import pydantic
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(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(TypeError):
my_sqlmdl_mdl.data = "bar"
Description
When setting a Field(allow_mutation=False)
in combination with the required validate_assignment = True
configuration on a SQLModel
, the framework doesn't even allow the field to be set in the initial construction of the model. This is allowed in pydantic
. I've attached a test suite that we should expect to pass, but it fails both the SQLModel
specific 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
Several issues have been reported about inconsistencies between sqlmodel and pydantic: https://github.com/tiangolo/sqlmodel/issues/87, https://github.com/tiangolo/sqlmodel/issues/230, etc. This particular issue is caused by these lines in main.py:
# Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy
# can handle them
# object.__setattr__(__pydantic_self__, '__dict__', values)
object.__setattr__(__pydantic_self__, "__fields_set__", fields_set)
for key, value in values.items():
setattr(__pydantic_self__, key, value)
The last line is assignment, which is forbidden by allow_mutation=False
. Unfortunately, the problem is not so easily fixed, because the handling of attributes is rather complex in sqlmodel. I have fixed it here, but it feels like duct tape.
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
Hi! Does the recent version of SQLModel (0.0.14) solves the issue? :)