Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGSync tries to find FK between unrelated tables. #142

Open
namoshizun opened this issue Jul 26, 2021 · 7 comments
Open

PGSync tries to find FK between unrelated tables. #142

namoshizun opened this issue Jul 26, 2021 · 7 comments

Comments

@namoshizun
Copy link

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"'

@toluaina
Copy link
Owner

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?

@namoshizun
Copy link
Author

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

@toluaina
Copy link
Owner

toluaina commented Aug 9, 2021

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

@namoshizun

@JacobReynolds
Copy link
Contributor

JacobReynolds commented Nov 17, 2021

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.

@loren
Copy link
Contributor

loren commented Aug 23, 2022

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 pushed a commit to loren/pgsync that referenced this issue Aug 23, 2022
- When `through_tables` are explicitly specified, the table gets added to the tree when it shouldn't.
@loren
Copy link
Contributor

loren commented Aug 23, 2022

this fixed it for me: #332

loren added a commit to loren/pgsync that referenced this issue Aug 24, 2022
@toluaina
Copy link
Owner

toluaina commented Sep 5, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants