You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
If you have two foreign keys in the same table, where both foreign keys point to the same external thing, only one of them shows up in pg_graphql.
To Reproduce
I have lists. Lists can contain works, or they can contain other lists. I want to get the total count of the items in the list.
CREATE TABLE IF NOT EXISTS public.lists (
list_id INTEGER PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS public.list_items (
id INTEGER PRIMARY KEY,
list_id INTEGER NOT NULL,
work_id INTEGER,
nested_list_id INTEGER,
FOREIGN KEY (list_id) REFERENCES public.lists (list_id) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO public.lists (list_id)
VALUES (1);
INSERT INTO public.list_items (id, list_id, work_id, nested_list_id)
VALUES
(1, 1, 101, NULL);
comment on table public.list_items is e'@graphql({"totalCount": {"enabled": true}})';
Now let's add a foreign key constraint for nested_list_id:
ALTER TABLE public.list_items
ADD CONSTRAINT fk_nested_list
FOREIGN KEY (nested_list_id) REFERENCES public.lists (list_id)
ON DELETE CASCADE ON UPDATE CASCADE;
When we run the GraphQL query again, we get: totalCount: 0.
I suspect what's happening is pg_graphql looks only at the item the foreign key is pointing to when generating names, and because both foreign keys are pointing to the same thing, the names are colliding and one of them is silently overwriting the other.
My current solution is to add a comment directive like this:
COMMENT ON CONSTRAINT fk_nested_list ON public.list_items IS E'@graphql({"local_name": "nestedListId", "foreign_name": "nestedList"})';
Running the GraphQL query again now gives us the correct totalCount: 1.
Expected behavior
I expected:
a warning somewhere, e.g. the linter, that two foreign keys pointing to the same thing would silently overwrite each other
them to not overwrite each other by e.g. using the name of the local column when generating the GraphQL schema (so e.g. "list_id" and "nested_list_id" would automatically have different names somehow)
The text was updated successfully, but these errors were encountered:
Describe the bug
If you have two foreign keys in the same table, where both foreign keys point to the same external thing, only one of them shows up in pg_graphql.
To Reproduce
I have lists. Lists can contain works, or they can contain other lists. I want to get the total count of the items in the list.
When I run this GraphQL query:
I get
totalCount: 1
Now let's add a foreign key constraint for nested_list_id:
When we run the GraphQL query again, we get:
totalCount: 0
.I suspect what's happening is pg_graphql looks only at the item the foreign key is pointing to when generating names, and because both foreign keys are pointing to the same thing, the names are colliding and one of them is silently overwriting the other.
This feels related to #502
My current solution is to add a comment directive like this:
Running the GraphQL query again now gives us the correct
totalCount: 1
.Expected behavior
I expected:
The text was updated successfully, but these errors were encountered: