diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-23 09:24:32 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-23 09:24:32 -0400 |
| commit | 8f5b65f4316e21d19b00266cdd9eded3d4ec51b2 (patch) | |
| tree | 02797202119399cf14eec1fcc2ecb6e2c551f7b3 /lib/sqlalchemy | |
| parent | 629ab1d9b8b86a2695e55543f4064df6e5775844 (diff) | |
| download | sqlalchemy-8f5b65f4316e21d19b00266cdd9eded3d4ec51b2.tar.gz | |
Don't assume m2o key is present in the dictionary
Fixed regression caused by new selectinload for many-to-one logic where
a primaryjoin condition not based on real foreign keys would cause
KeyError if a related object did not exist for a given key value on the
parent object.
Fixes: #4777
Change-Id: I4ba96318302be68abe0e8c3611684087a04a2322
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/strategies.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py index 82f40ea3b..8e8242c66 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -2380,7 +2380,12 @@ class SelectInLoader(AbstractRelationshipLoader, util.MemoizedSlots): } for key in chunk: - related_obj = data[key] + # for a real foreign key and no concurrent changes to the + # DB while running this method, "key" is always present in + # data. However, for primaryjoins without real foreign keys + # a non-None primaryjoin condition may still refer to no + # related object. + related_obj = data.get(key, None) for state, dict_, overwrite in our_states[key]: if not overwrite and self.key in dict_: continue @@ -2430,6 +2435,8 @@ class SelectInLoader(AbstractRelationshipLoader, util.MemoizedSlots): state, state_dict, collection[0] ) else: + # note that empty tuple set on uselist=False sets the + # value to None state.get_impl(self.key).set_committed_value( state, state_dict, collection ) |
