fix: set relationships when model_validate receives a dict - #2073
Open
latent-9 wants to merge 2 commits into
Open
fix: set relationships when model_validate receives a dict#2073latent-9 wants to merge 2 commits into
latent-9 wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sqlmodel_validate(which backsSQLModel.model_validateand the deprecatedfrom_orm/parse_obj) sets relationships with attribute access:But
use_objis a plaindictin the common case: it is the inputobjwhen a dict is passed, or{**obj, **update}whenupdate=is given.getattr(some_dict, "team", Undefined)never reads dict keys, so it always returnsUndefinedand the relationship is silently dropped, no error.This makes
model_validateinconsistent with both the constructor andmodel_validate(object):The sibling
sqlmodel_table_constructalready handles this correctly withvalues.get(key, Undefined).Fix
Read the relationship via
dict.getwhenuse_objis a dict, falling back togetattrotherwise (mirroringsqlmodel_table_construct). This also fixes theupdate=path.Test
Added
test_validate_dict_sets_relationship: a relationship passed in the dict tomodel_validate(and viaupdate=) 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.