postgraphile-plugin-batch-create-update-delete icon indicating copy to clipboard operation
postgraphile-plugin-batch-create-update-delete copied to clipboard

Delete requires a PK, but the input doesn't allow one

Open LMFinney opened this issue 5 years ago • 11 comments

I'm trying to use this for a batch deletion, but it doesn't work for me.

This is the input created for my table's batch deletion operation:

"""All input for the delete `mnDeleteWidget` mutation."""
input mnDeleteWidgetInput {
  """
  An arbitrary string value with no semantic meaning. Will be included in the
  payload verbatim. May be used to track mutations by the client.
  """
  clientMutationId: String

  """
  The one or many `Widget` to be deleted. You must provide the PK values!
  """
  mnPatch: [WidgetPatch!]
}

The comment about needing to provide the PK values is important. The problem is that WidgetPatch isn't the PK values. So, the operation fails. The runtime code demands a PK, but the API expects me to pass in something other than the PK.

LMFinney avatar Nov 05 '20 17:11 LMFinney

I suppose that this issue is related to the TODO here: https://github.com/tjmoses/postgraphile-plugin-many-create-update-delete/blob/master/src/PostGraphileManyDeletePlugin.ts#L232

LMFinney avatar Nov 05 '20 17:11 LMFinney

The comment from the link was for adding a custom input type for the actual plugin, vs. using the already existing patch input.

The mutation comment is talking about making sure to add "a" primary key attribute in the data you are sending over. Maybe I should have made it say "You must provide the PK values in the payload of sent array data". I do a primary key check here.

I have the pk value in the WHERE SQL clause here. I just have it as checking individual keys, so compound / composite keys aren't supported yet.

Anyways, I suppose you either are using composite keys or you didn't provide a single primary key? Post the exact error it shows and provide your table/example schema if you want me to take a quick look.

tjmoses avatar Nov 05 '20 23:11 tjmoses

Thanks for the very quick response. I'm on a different branch now, but I'll send more info tomorrow.

LMFinney avatar Nov 05 '20 23:11 LMFinney

Thanks. Here's the table schema:

CREATE TABLE IF NOT EXISTS public.widget
      (
          id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
          foo VARCHAR NOT NULL,
          bar DOUBLE PRECISION NOT NULL
      );

Here's the Smart Comment:

COMMENT ON TABLE public.widget IS
  E'@mncud\n Allow bulk deletes of widgets.

Here are the options I get when trying to bulk delete: image

image

I don't see a way to submit the PKs. Am I missing something or doing something wrong?

LMFinney avatar Nov 06 '20 02:11 LMFinney

Hmm, I'm not 100% positive as to why it doesn't show the id in the auto complete for the mnPatch. It may have something to do with the identity column your using for your serial pk and support with postgraphile. I suggest trying to just add the id in the data to be sent with the patch. If that doesn't work, maybe try the generated by default identity, and finally just a direct serial in your table schema (that's what I ran my tests on for this).

Also, you can open up your node_modules folder go to this plugin and run the lifecycle script while doing console logs if you want to see what is going on in the plugin better. I would be curious to see the output of the variable on line 106.

tjmoses avatar Nov 06 '20 03:11 tjmoses

Is this what you're looking for?

{
  uniqueConstraints: [
    {
      kind: 'constraint',
      id: '32216',
      name: 'widget_pkey',
      type: 'p',
      classId: '32209',
      foreignClassId: null,
      description: null,
      keyAttributeNums: [Array],
      foreignKeyAttributeNums: null,
      comment: null,
      tags: {},
      class: [Object],
      foreignClass: undefined,
      keyAttributes: [Array],
      foreignKeyAttributes: []
    }
  ],
  keyAttributeNums: [ 1 ],
  keyAttributes: [
    {
      kind: 'attribute',
      classId: '32209',
      num: 1,
      name: 'id',
      description: null,
      typeId: '23',
      typeModifier: null,
      isNotNull: true,
      hasDefault: false,
      identity: 'a',
      aclSelectable: true,
      aclInsertable: true,
      aclUpdatable: true,
      columnLevelSelectGrant: false,
      comment: null,
      tags: {},
      class: [Object],
      type: [Object],
      isIndexed: true,
      isUnique: true
    }
  ]
}

LMFinney avatar Nov 06 '20 18:11 LMFinney

It appears that key is fine then, from the data above showing it as a type 'p' (primary key). I bet the key doesn't show up in the patch input type, b/c it's a Generated always key and that can't be modified via a patch. Try some of the other suggestions and maybe even converting it to "generated by default identity"/"serial". I personally just pass in the variable to the mutation vs. adding it inline (shouldn't matter though).

Were you able to add in the id param? If so, what did the response give? If it still won't allow you to pass in that value, then it's got to be something with the patch types and that you're using the pk that can't be modified. If so, that's a limitation of this plugin until I/somebody can create a custom input type to get around this.

tjmoses avatar Nov 06 '20 23:11 tjmoses

I'm having the exact same problem here. I want to set isRead = true but I can't specify the PK of my table in the gql query, which is userAlertId.

image

This is the definition of my table:

CREATE TABLE "UserAlert"
(
  "UserAlertID"       BIGINT GENERATED ALWAYS AS IDENTITY,
  "UserID"            TEXT NOT NULL,
  "AlertID"           BIGINT NOT NULL REFERENCES "Alert" ON DELETE CASCADE,
  "IsRead"            BOOLEAN NOT NULL DEFAULT FALSE,
  PRIMARY KEY("UserAlertID")
);

tteguayco avatar Apr 26 '22 07:04 tteguayco

As pointed out by @tjmoses, using SERIAL instead of GENERATED ALWAYS AS IDENTITY works. Just tested with BIGSERIAL and it works as well. However it would be great to have it working with GENERATED ALWAYS AS IDENTITY as well, which is a more SQL-standard compliant statement.

tteguayco avatar Apr 26 '22 07:04 tteguayco

@tteguayco did you try using postgraphile directly to see if they generate the patch input type for this? If they do, that could mean this package may only need an updated version of postgraphile. I'm just not sure if they support as of now.

tjmoses avatar May 04 '22 18:05 tjmoses

I can verify that this is still the case for the latest version 4.13.0

rmlevangelio avatar Jun 28 '23 13:06 rmlevangelio