pgsync icon indicating copy to clipboard operation
pgsync copied to clipboard

parent/child and self-referenced structure

Open DanielZavacky opened this issue 4 years ago • 8 comments

PGSync version: 1.1.28

Postgres version: 13.1

Elasticsearch version: 7.10.1

Redis version: 6.2.1

Python version: 3.7

Problem Description:

Hi. I'm currently trying to work with pgsync and I'm facing two problems and I haven't found a way to solve them. Could you, please, tell me if it is even possible to do the following?

  1. I'm trying to define a schema that would map the parent/child association, where parent can have multiple children, child can have multiple children, and so on ... I have database table like this: parent_child Is there a way to create schema, which would generate something like this: parent_child_result_exp

  2. I'm trying to define a schema that would map self-referenced association, where item can have array of alternative items I have a database like this: self_ref Is there a way to create schema, which would generate something like this: self_ref_data

DanielZavacky avatar Mar 23 '21 16:03 DanielZavacky

Yes you can do 1 and 2.

For 1) you can have a look at the books example here

For 2) you just need to define a relationship of type one_to_many

toluaina avatar Mar 26 '21 09:03 toluaina

I can't get 1. scenario (parent/child) working. I have this schema and this DB table: schema_parent_child, but it will create this documents in elasticsearch: elastic_parent_child

  1. scenario is resolved by foreign_key property

DanielZavacky avatar Apr 23 '21 11:04 DanielZavacky

I'll be honest self-referential relationships have not been fully explored. Let alone self-referential user-defined foreign key relationships.

Are you sure this shouldn't be

"foreign_key": {
      "child": ["id"],
      "parent": ["parent_id"]
 }

In any case, I feel a self-referencing relationship should be handled as a special case. The complexity for unbounded queries will need to be resolved with CTE's

toluaina avatar Apr 24 '21 22:04 toluaina

Yes, I tried both

"foreign_key": {
      "child": ["id"],
      "parent": ["parent_id"]
 }

and

"foreign_key": {
      "child": ["parent_id"],
      "parent": ["id"]
 }

and neither worked as expected.

Yes, recursive CTE is probably the only way to do it. If I could be helpful in any way, just let me know

DanielZavacky avatar Apr 26 '21 11:04 DanielZavacky

Any workaround? I have the same issue.

shambarick avatar Jul 01 '21 21:07 shambarick

Same problem here

mmbfreitas avatar Nov 17 '22 15:11 mmbfreitas