From 042bccd2e4d6fbfcdf70ede760b29f78771f4b22 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 14 Jun 2022 15:41:31 -0400 Subject: pickle mutable parents according to key Fixed bug in :class:`.Mutable` where pickling and unpickling of an ORM mapped instance would not correctly restore state for mappings that contained multiple :class:`.Mutable`-enabled attributes. Fixes: #8133 Change-Id: I508763e0df0d7a624e1169f9a46d7f25404add1e --- lib/sqlalchemy/ext/mutable.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/ext/mutable.py b/lib/sqlalchemy/ext/mutable.py index ba7f9b0a4..1ae5aee8b 100644 --- a/lib/sqlalchemy/ext/mutable.py +++ b/lib/sqlalchemy/ext/mutable.py @@ -355,6 +355,7 @@ pickling process of the parent's object-relational state so that the :meth:`MutableBase._parents` collection is restored to all ``Point`` objects. """ +from collections import defaultdict import weakref from .. import event @@ -496,12 +497,12 @@ class MutableBase: val = state.dict.get(key, None) if val is not None: if "ext.mutable.values" not in state_dict: - state_dict["ext.mutable.values"] = [] - state_dict["ext.mutable.values"].append(val) + state_dict["ext.mutable.values"] = defaultdict(list) + state_dict["ext.mutable.values"][key].append(val) def unpickle(state, state_dict): if "ext.mutable.values" in state_dict: - for val in state_dict["ext.mutable.values"]: + for val in state_dict["ext.mutable.values"][key]: val._parents[state] = key event.listen(parent_cls, "load", load, raw=True, propagate=True) -- cgit v1.2.1