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

Multiple foreign keys pointing to the same thing overwrite each other silently #541

Closed
pawarren opened this issue Jul 9, 2024 · 1 comment
Assignees
Labels
triage-required Pending triage from maintainers

Comments

@pawarren
Copy link

pawarren commented Jul 9, 2024

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}})';

When I run this GraphQL query:

query {
  listsCollection(filter: {list_id: {eq: 1}}) {
    edges {
      node {
        list_id
        list_itemsCollection {
          totalCount
        }
      }
    }
  }
}

I get totalCount: 1

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.

This feels related to #502

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:

  1. a warning somewhere, e.g. the linter, that two foreign keys pointing to the same thing would silently overwrite each other
  2. 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)
@olirice
Copy link
Contributor

olirice commented Jul 10, 2024

This part is a dupe of #345
resolution available in that issue

We will likely also split these as customers and customers2 in a future release rather than silently overwriting by default

@olirice olirice closed this as completed Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage-required Pending triage from maintainers
Projects
None yet
Development

No branches or pull requests

2 participants