From e10f01e9f764acb0cc0d030fa7456ada775c6932 Mon Sep 17 00:00:00 2001 From: Nicolas Delaby Date: Tue, 23 Apr 2019 18:39:14 +0200 Subject: [PATCH] Fix for sqlalchemy 1.3 Accessing a collection not yet set doesn't raise AttributeError anymore --- flask_potion/contrib/alchemy/manager.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/flask_potion/contrib/alchemy/manager.py b/flask_potion/contrib/alchemy/manager.py index 6aaaa40..f80abfb 100644 --- a/flask_potion/contrib/alchemy/manager.py +++ b/flask_potion/contrib/alchemy/manager.py @@ -328,10 +328,18 @@ def relation_add(self, item, attribute, target_resource, target_item): def relation_remove(self, item, attribute, target_resource, target_item): before_remove_from_relation.send(self.resource, item=item, attribute=attribute, child=target_item) try: - getattr(item, attribute).remove(target_item) - after_remove_from_relation.send(self.resource, item=item, attribute=attribute, child=target_item) + collection = getattr(item, attribute) except ValueError: pass # if the relation does not exist, do nothing + else: + if target_item in collection: + collection.remove(target_item) + after_remove_from_relation.send( + self.resource, + item=item, + attribute=attribute, + child=target_item, + ) def commit(self): session = self._get_session()