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
I used the search to find a similar question and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
my_object.sqlmodel_update(my_update_object)
Description
Use sqlmodel_update on some SQLModel instance, in my case passing in a BaseModel instance. This then calls get_model_fields on the BaseModel instance. This function returns model.fields which leads to a deprecation warning from pydantic:
Accessing this attribute on the instance is deprecated, and will be removed in Pydantic V3. Instead, you should access this attribute from the model class. Deprecated in Pydantic V2.11 to be removed in V3.0.
Easy way (though possibly not the nicest way) to fix this particular issue is to change sqlmodel_update to:
def sqlmodel_update(
self: _TSQLModel,
obj: Union[Dict[str, Any], BaseModel],
*,
update: Union[Dict[str, Any], None] = None,
) -> _TSQLModel:
use_update = (update or {}).copy()
if isinstance(obj, dict):
for key, value in {**obj, **use_update}.items():
if key in get_model_fields(type(self)):
setattr(self, key, value)
elif isinstance(obj, BaseModel):
for key in get_model_fields(type(obj)):
if key in use_update:
value = use_update.pop(key)
else:
value = getattr(obj, key)
setattr(self, key, value)
for remaining_key in use_update:
if remaining_key in get_model_fields(type(self)):
value = use_update.pop(remaining_key)
setattr(self, remaining_key, value)
else:
raise ValueError(
"Can't use sqlmodel_update() with something that "
f"is not a dict or SQLModel or Pydantic model: {obj}"
)
return self
Of course def get_model_fields(model: InstanceOrType[BaseModel]) should also be updated to def get_model_fields(model: Type[BaseModel])
Note: Several other deprecation warnings I noticed as well, though I don't think these ones are new as of pydantic 2.11... And these ones aren't causing my tests to fail on deprecation warnings.
get_fields_set is using .__fields_set__ which should be updated to .model_fields_set
reacted with thumbs up emojireacted with thumbs down emojireacted with laugh emojireacted with hooray emojireacted with confused emojireacted with heart emojireacted with rocket emojireacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
Use
sqlmodel_update
on some SQLModel instance, in my case passing in a BaseModel instance.This then calls
get_model_fields
on the BaseModel instance.This function returns model.fields which leads to a deprecation warning from pydantic:
Easy way (though possibly not the nicest way) to fix this particular issue is to change
sqlmodel_update
to:Of course
def get_model_fields(model: InstanceOrType[BaseModel])
should also be updated todef get_model_fields(model: Type[BaseModel])
Note: Several other deprecation warnings I noticed as well, though I don't think these ones are new as of pydantic 2.11... And these ones aren't causing my tests to fail on deprecation warnings.
.__fields_set__
which should be updated to.model_fields_set
Operating System
Linux
Operating System Details
python-slim container
SQLModel Version
0.0.24
Python Version
3.12.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions