From f45df5d4d3d3fd8ed40e5d3d218838a7a99f2903 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 29 Apr 2022 23:24:28 +0200 Subject: [PATCH] Explictly cast entity IDs to strings when coalescing entity updates Some plugins may represent entity IDs as integers, while the database maps external IDs to strings. This may result in entities being incorrectly mapped during merging. Casting to string prevents these type-related ambiguities. --- platypush/entities/_engine.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/platypush/entities/_engine.py b/platypush/entities/_engine.py index 13f69d4cc..3ed383ef2 100644 --- a/platypush/entities/_engine.py +++ b/platypush/entities/_engine.py @@ -172,7 +172,12 @@ class EntitiesEngine(Thread): self, session: Session, entities: Iterable[Entity] ) -> Iterable[Entity]: existing_entities = { - (entity.external_id or entity.name, entity.plugin): entity + ( + str(entity.external_id) + if entity.external_id is not None + else entity.name, + entity.plugin, + ): entity for entity in session.query(Entity) .filter( or_( @@ -194,7 +199,13 @@ class EntitiesEngine(Thread): return [ existing_entities.get( - (entity.external_id or entity.name, entity.plugin), None + ( + str(entity.external_id) + if entity.external_id is not None + else entity.name, + entity.plugin, + ), + None, ) for entity in entities ] @@ -207,8 +218,8 @@ class EntitiesEngine(Thread): for col in columns: if col == 'meta': existing_entity.meta = { # type: ignore - **(existing_entity.meta or {}), - **(entity.meta or {}), + **(existing_entity.meta or {}), # type: ignore + **(entity.meta or {}), # type: ignore } elif col not in ('id', 'created_at'): setattr(existing_entity, col, getattr(entity, col))