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

Partial unique indices that overlap with an existing foreign key silently overwrite the existing foreign key connection #542

Closed
pawarren opened this issue Jul 9, 2024 · 0 comments · Fixed by #543
Assignees
Labels
bug Something isn't working

Comments

@pawarren
Copy link

pawarren commented Jul 9, 2024

Describe the bug
When I have a table that has a foreign key reference, and then I create a unique partial index on the same field, the

To Reproduce
I have a table called "works" and a table called "readthroughs". Each work can have many readthroughs. I want to ensure there is at most one in progress readthrough per work at a given time.

Create tables and insert the initial values:

CREATE TABLE IF NOT EXISTS public.works (
    work_id INTEGER,
    PRIMARY KEY (work_id)
);

CREATE TABLE IF NOT EXISTS public.readthroughs (
    readthrough_id INTEGER NOT NULL,
    work_id INTEGER NOT NULL,
    status TEXT NOT NULL,
    PRIMARY KEY (readthrough_id),
    FOREIGN KEY (work_id) REFERENCES public.works (work_id) ON DELETE CASCADE ON UPDATE CASCADE
);

INSERT INTO public.works (work_id) VALUES (1);

INSERT INTO public.readthroughs (readthrough_id, work_id, status)
VALUES (1, 1, 'in_progress');

COMMENT ON TABLE public.readthroughs IS E'@graphql({"totalCount": {"enabled": true}})';

When I look at the GraphiQL docs, I see:
Screenshot 2024-07-09 at 12 54 30 PM

I can run queries like this successfully:

{
  worksCollection(filter: {work_id: {eq: 1}}) {
    edges {
      node {
        work_id
        readthroughsCollection {
          totalCount
        }
      }
    }
  }
}

Now I add a unique partial index:

CREATE UNIQUE INDEX idx_unique_active_readthrough ON public.readthroughs (work_id)
WHERE status IN ('in_progress');

The GraphiQL docs now look like this:
Screenshot 2024-07-09 at 12 55 34 PM

And I can no longer use readthroughsCollection in my GraphQL queries.

This feels like it might be related to #541 and #502, i.e. pg_graphql generating readthroughCollection based on the foreign key but then overwriting it because of the index.

Expected behavior
Creating a unique partial index shouldn't screw with existing Collections

@pawarren pawarren added the triage-required Pending triage from maintainers label Jul 9, 2024
@olirice olirice added bug Something isn't working and removed triage-required Pending triage from maintainers labels Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants