From 0ed331afab2d6d51720a7d2cb891b2c0d124d3d7 Mon Sep 17 00:00:00 2001 From: Loren Siebert Date: Tue, 23 Aug 2022 14:40:56 -0700 Subject: [PATCH 1/2] Fixes #142 - When `through_tables` are explicitly specified, the table gets added to the tree when it shouldn't. --- pgsync/node.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pgsync/node.py b/pgsync/node.py index cf726aba..b838a81d 100644 --- a/pgsync/node.py +++ b/pgsync/node.py @@ -288,8 +288,6 @@ def build(self, data: dict) -> Node: ) self.tables.add(node.table) - for through_node in node.relationship.through_nodes: - self.tables.add(through_node.table) for child in data.get("children", []): node.add_child(self.build(child)) From e6b842c07bef1393b0d70b1bb7b4d29a339e782c Mon Sep 17 00:00:00 2001 From: Loren Siebert Date: Wed, 24 Aug 2022 15:51:09 -0700 Subject: [PATCH 2/2] Allow primary key to be specified for relationship Workaround for Prisma-managed implicit through tables that have no primary key defined - https://github.com/prisma/prisma/issues/11110 --- pgsync/constants.py | 1 + pgsync/node.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pgsync/constants.py b/pgsync/constants.py index 3ea19862..9cf95728 100644 --- a/pgsync/constants.py +++ b/pgsync/constants.py @@ -38,6 +38,7 @@ "through_tables", "type", "variant", + "primary_key", ] # Relationship foreign keys diff --git a/pgsync/node.py b/pgsync/node.py index b838a81d..9d11eee1 100644 --- a/pgsync/node.py +++ b/pgsync/node.py @@ -64,6 +64,7 @@ def __post_init__(self): self.through_tables: List[str] = self.relationship.get( "through_tables", [] ) + self.primary_key: List[str] = self.relationship.get("primary_key", []) self.through_nodes: List[Node] = [] if not set(self.relationship.keys()).issubset( @@ -97,7 +98,7 @@ def __post_init__(self): def __str__(self): return ( - f"relationship: {self.variant}.{self.type}:{self.through_tables}" + f"relationship: {self.variant}.{self.type}:{self.through_tables}.{self.primary_key}" ) @@ -149,7 +150,7 @@ def __post_init__(self): table=through_table, schema=self.schema, parent=self, - primary_key=[], + primary_key=self.relationship.primary_key, ) )