pgsync icon indicating copy to clipboard operation
pgsync copied to clipboard

PGSync tries to find FK between unrelated tables.

Open namoshizun opened this issue 3 years ago • 7 comments

PGSync version: v2.1.1

Postgres version: v11.1

Elasticsearch version: v7.11.2

Redis version: v5

Python version: v3.8

Problem Description:

Using this schema, while bootstrap and pgsync commands run without issues, I am getting the following error when actually create a Tenant database record. Look like pgsync has mistakenly tried to correlate the UserRoles medium table with Role's foreign key entity District?

Error Message (if any):

Exception in thread Thread-23:
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.8/site-packages/pgsync/sync.py", line 860, in poll_redis
    self.on_publish(payloads)
  File "/usr/local/lib/python3.8/site-packages/pgsync/sync.py", line 927, in on_publish
    self.sync_payloads(_payload)
  File "/usr/local/lib/python3.8/site-packages/pgsync/sync.py", line 829, in sync_payloads
    for doc in self._payloads(payloads):
  File "/usr/local/lib/python3.8/site-packages/pgsync/sync.py", line 645, in _payloads
    filters = self._insert(
  File "/usr/local/lib/python3.8/site-packages/pgsync/sync.py", line 339, in _insert
    foreign_keys = self.query_builder._get_foreign_keys(
  File "/usr/local/lib/python3.8/site-packages/pgsync/querybuilder.py", line 45, in _get_foreign_keys
    for key, value in get_foreign_keys(through, node_b).items():
  File "/usr/local/lib/python3.8/site-packages/pgsync/base.py", line 961, in get_foreign_keys
    for table, columns in _get_foreign_keys(
  File "/usr/local/lib/python3.8/site-packages/pgsync/base.py", line 920, in _get_foreign_keys
    raise ForeignKeyError(
pgsync.exc.ForeignKeyError: 'No foreign key relationship between "public.saas_api_user_roles" and "public.saas_api_district"'

namoshizun avatar Jul 26 '21 09:07 namoshizun

There needs to be a direct or indirect relationship between all tables in your schemas. Can you please provide more details of your database schema?

toluaina avatar Jul 29 '21 20:07 toluaina

There needs to be a direct or indirect relationship between all tables in your schemas. Can you please provide more details of your database schema?

I've hyperlinked the schema source code in the issue description, or this link: https://gist.github.com/namoshizun/3f86d97cd0b4ce72b779ed913a4fa849

namoshizun avatar Jul 31 '21 02:07 namoshizun

Sorry I meant the database schema itself. You can send this to me directly if you have any reservations.

@namoshizun

toluaina avatar Aug 09 '21 19:08 toluaina

Bumping this, I'm experiencing the same issue. It seems to happen when you add children to a node that has a through_table property. For example this child seems to be using the through_table table of its parent instead of the table of its parent.

I haven't been able to find exact reproduction steps, the initial bootstrap works, but at some point it breaks during a bulk update.

This seems to be a duplicate of #199.

JacobReynolds avatar Nov 17 '21 20:11 JacobReynolds

I appear to be running into this as well. Initial bootstrap and sync work, but adding a new record on my Book<->Author through table errors with

pgsync.exc.ForeignKeyError: 'No foreign key relationship between "public.BookAuthor" and "public.BookAuthor"'

Adding just a new Book works. Adding just a new Author works. It's adding a Book with an Author that is failing. The op causing it is

DEBUG:pgsync.sync: tg_op: INSERT table: public.BookAuthor

The last bit of the stack trace is

File "/Users/me/opt/anaconda3/lib/python3.9/site-packages/pgsync/sync.py", line 824, in _payloads
    filters = self._insert_op(
  File "/Users/me/opt/anaconda3/lib/python3.9/site-packages/pgsync/sync.py", line 491, in _insert_op
    foreign_keys = self.query_builder._get_foreign_keys(
  File "/Users/me/opt/anaconda3/lib/python3.9/site-packages/pgsync/querybuilder.py", line 68, in _get_foreign_keys
    for key, value in get_foreign_keys(through_node, node_b).items():
  File "/Users/me/opt/anaconda3/lib/python3.9/site-packages/pgsync/base.py", line 890, in get_foreign_keys
    for table, columns in _get_foreign_keys(
  File "/Users/me/opt/anaconda3/lib/python3.9/site-packages/pgsync/base.py", line 850, in _get_foreign_keys
    raise ForeignKeyError(

I'll try to dig into the code to see what it's expecting but would be happy to hear suggestions or workarounds.

loren avatar Aug 23 '22 17:08 loren

this fixed it for me: https://github.com/toluaina/pgsync/pull/332

loren avatar Aug 23 '22 21:08 loren

Sorry for taking a while to address this. Can you please try against the main branch. This should be resolved now.

toluaina avatar Sep 05 '22 16:09 toluaina