toluaina / pgsync

Postgres to Elasticsearch/OpenSearch sync
https://pgsync.com
MIT License
1.11k stars 174 forks source link

QueryBuilder fails when faces 3 layer deep relationships of certain kind #397

Closed MeRuslan closed 1 year ago

MeRuslan commented 1 year ago

PGSync version: 2.3.3

Postgres version: NA

Elasticsearch version: NA

Redis version: NA

Python version: NA

Problem Description: When there's base - one-to-one child - one to many with through kind of relationship, pgsync calls _non_through path for the one-to-one child which in turn tries to handle the one to many with through (which is a child of a first level one-to-one child) connection but fails,

It fails here, fails to find foreign keys foreign_key_columns: list = self._get_column_foreign_keys( child._subquery.columns, foreign_keys, )

Maybe self._get_column_foreign_keys is not thorough enough to correctly link these entities in such a case.

Thus rendering pgsync not usable for that kind of a schema.

Not sure where exactly to fix it.

Error Message (if any):

    for i in range(len(foreign_key_columns)):
TypeError: object of type 'NoneType' has no len()
MeRuslan commented 1 year ago

After giving it another look, it seems like table argument should be passed there, in which case it would resolve correctly.

            table = (
                child.relationship.throughs[0].table if child.relationship.throughs
                else None
            )
            foreign_key_columns: list = self._get_column_foreign_keys(
                child._subquery.columns,
                foreign_keys,
                table=table,
                schema=node.schema,
            )

That simple correction worked for me. Would appreciate it if you @toluaina could have had a look.

MeRuslan commented 1 year ago

working on a pr