From b8cf0c7a2f367faedc9beccae8946a0e2b33765e Mon Sep 17 00:00:00 2001 From: scafa91 Date: Tue, 6 Dec 2022 16:31:30 +0000 Subject: [PATCH 1/2] fix handling through table --- pgsync/sync.py | 54 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/pgsync/sync.py b/pgsync/sync.py index 20f9cd8c..0302c4ed 100644 --- a/pgsync/sync.py +++ b/pgsync/sync.py @@ -464,22 +464,46 @@ def _insert_op( ) raise - # set the parent as the new entity that has changed - foreign_keys = self.query_builder._get_foreign_keys( - node.parent, - node, - ) + if node in node.parent.relationship.throughs: + foreign_keys = self.query_builder._get_foreign_keys( + node.parent, + node, + ) - for payload in payloads: - for i, key in enumerate(foreign_keys[node.name]): - if key == foreign_keys[node.parent.name][i]: - filters[node.parent.table].append( - { - foreign_keys[node.parent.name][ - i - ]: payload.data[key] - } - ) + for payload in payloads: + primary_values: list = [ + payload.data[key] for key in node.model.primary_keys + ] + + for i, key in enumerate(foreign_keys[node.parent.name]): + for val in primary_values: + filters[node.parent.table].append( + {foreign_keys[node.parent.name][i]: val}, + ) + + for pk in self.tree.root.model.primary_keys: + for val in primary_values: + filters[self.tree.root.table].append( + {pk: val} + ) + + else: + # set the parent as the new entity that has changed + foreign_keys = self.query_builder._get_foreign_keys( + node.parent, + node, + ) + + for payload in payloads: + for i, key in enumerate(foreign_keys[node.name]): + if key == foreign_keys[node.parent.name][i]: + filters[node.parent.table].append( + { + foreign_keys[node.parent.name][ + i + ]: payload.data[key] + } + ) else: From 0533d628b83855d4eacfcbc570d3c461f711c955 Mon Sep 17 00:00:00 2001 From: scafa91 Date: Wed, 7 Dec 2022 14:38:04 +0000 Subject: [PATCH 2/2] fix --- pgsync/sync.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgsync/sync.py b/pgsync/sync.py index 0302c4ed..3e5fb835 100644 --- a/pgsync/sync.py +++ b/pgsync/sync.py @@ -469,7 +469,7 @@ def _insert_op( node.parent, node, ) - + filters[node.parent.name] = [] for payload in payloads: primary_values: list = [ payload.data[key] for key in node.model.primary_keys