Skip to content

Bug with children tables when they have multiple foreign keys to the parent table #622

@MeRuslan

Description

@MeRuslan

For example the following schema used to work with pgsync 4.2.0.
Pgsync 7.0.5 fails, it looks like it doesn't use the explicit foreign key given to it to perform the join?

{
        "database": "postgres",
        "index": "user_index",
        "nodes": {
            "table": "user",
            "columns": [
                "id",
                "username",
                "email",
            ],
            "children": [
                {
                    "table": "user_subscription",
                    "columns": [
                        "subscription_id"
                    ],
                    "relationship": {
                        "type": "one_to_many",
                        "variant": "scalar",
                        "foreign_key": {
                            "child": [
                                "subscriber_id"
                            ],
                            "parent": [
                                "id"
                            ]
                        }
                    }
                }
            ]
        }
    }

It fails with

2026-02-16 00:00:00 Traceback (most recent call last):
2026-02-16 00:00:00   File "/app/.venv/bin/pgsync", line 7, in <module>
2026-02-16 00:00:00     sync.main()
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/click/core.py", line 1161, in __call__
2026-02-16 00:00:00     return self.main(*args, **kwargs)
2026-02-16 00:00:00            ^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/click/core.py", line 1082, in main
2026-02-16 00:00:00     rv = self.invoke(ctx)
2026-02-16 00:00:00          ^^^^^^^^^^^^^^^^
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/click/core.py", line 1443, in invoke
2026-02-16 00:00:00     return ctx.invoke(self.callback, **ctx.params)
2026-02-16 00:00:00            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/click/core.py", line 788, in invoke
2026-02-16 00:00:00     return __callback(*args, **kwargs)
2026-02-16 00:00:00            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/pgsync/sync.py", line 2346, in main
2026-02-16 00:00:00     sync.pull()
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/pgsync/sync.py", line 1877, in pull
2026-02-16 00:00:00     self.search_client.bulk(
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/pgsync/search_client.py", line 139, in bulk
2026-02-16 00:00:00     self._bulk(
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/pgsync/search_client.py", line 197, in _bulk
2026-02-16 00:00:00     for ok, info in self.parallel_bulk(
2026-02-16 00:00:00                     ^^^^^^^^^^^^^^^^^^^
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/elasticsearch/helpers/actions.py", line 472, in parallel_bulk
2026-02-16 00:00:00     for result in pool.imap(
2026-02-16 00:00:00                   ^^^^^^^^^^
2026-02-16 00:00:00   File "/usr/local/lib/python3.12/multiprocessing/pool.py", line 873, in next
2026-02-16 00:00:00     raise value
2026-02-16 00:00:00   File "/usr/local/lib/python3.12/multiprocessing/pool.py", line 125, in worker
2026-02-16 00:00:00     result = (True, func(*args, **kwds))
2026-02-16 00:00:00                     ^^^^^^^^^^^^^^^^^^^
2026-02-16 00:00:00   File "/usr/local/lib/python3.12/multiprocessing/pool.py", line 144, in _helper_reraises_exception
2026-02-16 00:00:00     raise ex
2026-02-16 00:00:00   File "/usr/local/lib/python3.12/multiprocessing/pool.py", line 391, in _guarded_task_generation
2026-02-16 00:00:00     for i, x in enumerate(iterable):
2026-02-16 00:00:00                 ^^^^^^^^^^^^^^^^^^^
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/elasticsearch/helpers/actions.py", line 155, in _chunk_actions
2026-02-16 00:00:00     for action, data in actions:
2026-02-16 00:00:00                         ^^^^^^^
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/pgsync/sync.py", line 1488, in sync
2026-02-16 00:00:00     self.query_builder.build_queries(
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/pgsync/querybuilder.py", line 1006, in build_queries
2026-02-16 00:00:00     self._non_through(node)
2026-02-16 00:00:00   File "/app/.venv/lib/python3.12/site-packages/pgsync/querybuilder.py", line 964, in _non_through
2026-02-16 00:00:00     == node.parent.model.c[parent_foreign_key_columns[i]]
2026-02-16 00:00:00                            ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
2026-02-16 00:00:00 IndexError: list index out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions