| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
sqlmodel_validate read each relationship with getattr(use_obj, key), but use_obj is a dict when a dict is passed to model_validate (or when update is given, which produces a merged dict). getattr never finds dict keys, so the relationship was silently dropped, unlike the constructor and model_validate(object) which set it. Read via dict.get for dict input, mirroring sqlmodel_table_construct, and add a regression test.
| Back | FazBrowse Home | New Git URL |
Summary
sqlmodel_validate (which backs SQLModel.model_validate and the deprecated from_orm/parse_obj) sets relationships with attribute access:
But use_obj is a plain dict in the common case: it is the input obj when a dict is passed, or {**obj, **update} when update= is given. getattr(some_dict, "team", Undefined) never reads dict keys, so it always returns Undefined and the relationship is silently dropped, no error.
This makes model_validate inconsistent with both the constructor and model_validate(object):
The sibling sqlmodel_table_construct already handles this correctly with values.get(key, Undefined).
Fix
Read the relationship via dict.get when use_obj is a dict, falling back to getattr otherwise (mirroring sqlmodel_table_construct). This also fixes the update= path.
Test
Added test_validate_dict_sets_relationship: a relationship passed in the dict to model_validate (and via update=) must be set, matching the constructor. It fails before the change and passes after. The existing validation tests only pass scalar fields, so this path was uncovered.