postgraphile-plugin-nested-mutations icon indicating copy to clipboard operation
postgraphile-plugin-nested-mutations copied to clipboard

Tables that relate to themselves

Open mitswan opened this issue 4 years ago • 3 comments

I have noticed tables that reference themselves will only work one way with this plugin.

In the example scenario, you can nested insert Child -> Parent OR Parent -> Child

create table app.parent (
  id serial primary key,
  name text not null
);

create table app.child (
  id serial primary key,
  parent_id integer,
  name text not null,
  constraint child_parent_fkey foreign key (parent_id)
    references app.parent (id)
);

But if you make it a single person table it will only let you nested insert Child -> Parent

create table app.person (
  id serial primary key,
  parent_id integer,
  name text not null,
  constraint person_parent_fkey foreign key (parent_id)
    references app.person(id)
);

mitswan avatar Jan 29 '20 00:01 mitswan

I am facing the same problem, it must come from an inflection issue, I'll try to look into it.

wafokevin avatar Oct 08 '20 14:10 wafokevin

I digged a little bit into this. I modified the package with https://www.npmjs.com/package/patch-package. Inside PostgraphileNestedTypesPlugin l.177, I modified the isForward condition. It was masking some reverse relations when their constraints were related to themselves, eg : const isForward = constraint.classId === table.id I switched it to : const isForward = constraint.classId === table.id && constraint.classId !== constraint.foreignClassId; To allow these special kind of constraints to work.

Untested, use at your own risk.

I would be glad to make a PR but the repo owner doesn't seem to be maintaining it.

Eldow avatar Dec 22 '20 14:12 Eldow

Sorry for the delay. Changing the test for isForward just swaps the problem around -- you now get the the parent > child nested mutation, but lose the child > parent nested mutation. To fix this properly needs a bit of a refactor of the code I think. I've added a test for the currently working direction (child > parent), and I'll do some refactoring to make the other direction work right.

mlipscombe avatar Mar 26 '21 13:03 mlipscombe