-
-
Notifications
You must be signed in to change notification settings - Fork 208
Description
Hey,
I'm trying to get tags indexed on a certain type (one to many), I've ran in to some blockers and can't get this working, it seems the reason it's not working is because of the "through table" do not contain a foreign key to the user in the example below.
I've created a minimal example that shows the issue I'm having.
The reason we do not have a foreign key is because of implementation details in a larger project, so that can sadly not be changed.
Example 1
The postgres tables:
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
);
CREATE TABLE IF NOT EXISTS tag (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS user_tag (
id SERIAL PRIMARY KEY,
user_id INTEGER,
tag_id INTEGER REFERENCES tag(id)
);The pgsync schema:
[
{
"database": "postgres",
"index": "users",
"nodes": {
"table": "users",
"columns": ["id", "name", "email"],
"children": [
{
"table": "user_tag",
"columns": [],
"relationship": {
"variant": "scalar",
"type": "one_to_many",
"foreign_key": {
"child": ["user_id"],
"parent": ["id"]
}
},
"children": [
{
"table": "tag",
"columns": ["name"],
"label": "tag",
"relationship": {
"variant": "scalar",
"type": "one_to_one",
"foreign_key": {
"child": ["tag_id"],
"parent": ["id"]
}
}
}
]
}
]
}
}
]
In this example we end up with user_tag.id in the elastic documents but we want the tag.name, like the following:
"user_tag": [
1,
2,
3
],Example 2
This is another approach I've tried using through_tables
[
{
"database": "postgres",
"index": "users",
"nodes": {
"table": "users",
"columns": ["id", "name", "email"],
"children": [
{
"table": "tag",
"columns": ["name"],
"relationship": {
"variant": "scalar",
"type": "one_to_many",
"through_tables": ["user_tag"]
},
"transform": {
"rename": {
"name": "tag"
}
}
}
]
}
}
]This one failes with a clear error about foreign keys missing, I also tried adding foreign_key manually in this case without any success.
for table, cols in self.get_foreign_keys(through, node.parent).items():
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/pgsync/querybuilder.py", line 273, in get_foreign_keys
raise ForeignKeyError(
pgsync.exc.ForeignKeyError: 'No foreign key relationship between public.user_tag and public.users'
So to my final question, am I just missing something or is this not possible? Do you have any recommended workarounds to the issue?