From d7278857e59ee4ee1370c61575accfc60d9d9a24 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 23 Oct 2022 00:28:42 +0200 Subject: [PATCH] Ensure that no records with duplicate key exist within an SQLAlchemy session before flushing --- platypush/entities/_engine.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/platypush/entities/_engine.py b/platypush/entities/_engine.py index fef07a054..c258ea09f 100644 --- a/platypush/entities/_engine.py +++ b/platypush/entities/_engine.py @@ -228,13 +228,15 @@ class EntitiesEngine(Thread): return existing_entity - new_entities = [] + def entity_key(entity: Entity): + return ((entity.external_id or entity.name), entity.plugin) + + new_entities = {} entities_map = {} # Get the latest update for each ((id|name), plugin) record for e in entities: - key = ((e.external_id or e.name), e.plugin) - entities_map[key] = e + entities_map[entity_key(e)] = e # Retrieve existing records and merge them for i, entity in enumerate(entities): @@ -242,9 +244,9 @@ class EntitiesEngine(Thread): if existing_entity: entity = merge(entity, existing_entity) - new_entities.append(entity) + new_entities[entity_key(entity)] = entity - return new_entities + return list(new_entities.values()) def _process_entities(self, *entities: Entity): with self._get_db().get_session() as session: