graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

Unique index constraint appears in studio but fails when used in query, both by admin and user

Open georgiosd opened this issue 5 years ago • 7 comments

Example query:

mutation {
  insert_table(
    	objects: [{
    key: "test",
    product_id: 1,
    value: "test",
  }],
    on_conflict: {
      constraint: unique_index_name_as_appear_in_mutation_root,
      update_columns: value
    }
  ) {
    affected_rows
  }
}

Error:

{
  "errors": [
    {
      "extensions": {
        "path": "$.selectionSet.insert_table.args.objects",
        "code": "constraint-error"
      },
      "message": "constraint \"unique_index_name_as_appear_in_mutation_root\" for table \"insert_table\" does not exist"
    }
  ]
}

georgiosd avatar Feb 28 '20 10:02 georgiosd

@georgiosd Could you give us a sample repro so we can try it on our end?

tirumaraiselvan avatar Mar 04 '20 09:03 tirumaraiselvan

@tirumaraiselvan I'm sorry but currently very limited on time...

You need to create a unique index in postgres for { product_id, key, value } and then try the above query

georgiosd avatar Mar 04 '20 13:03 georgiosd

I think this might be related to https://github.com/hasura/graphql-engine/issues/3666#issuecomment-572419941

rikinsk avatar Mar 05 '20 10:03 rikinsk

This was caused by creating a unique index in Postgres instead of a constraint.

We added the unique index using rails which makes a "unique index" that does not have a contraint even though that is not considered proper per https://www.postgresql.org/message-id/CAO8h7BJMX5V1TqzScTx2Nr1jH5iUFG8A071y-g1b_kdzpu9PDw%40mail.gmail.com -- and also sorta a design flaw in postgres that it even allows this, it seems like.

Thanks!

georgiosd avatar May 01 '20 17:05 georgiosd

I faced this issue this morning. Seems like a potential bug in Hasura that the constraint is offered as part of the schema if it doesn't work.

carlpaten avatar Nov 04 '21 14:11 carlpaten

I experienced this issue today, any updates?

furkan-kajabi avatar Oct 04 '22 12:10 furkan-kajabi

Have the same issue, using unique index with where filter is not recognized by hasura as constraint

ilijaNL avatar Oct 17 '22 10:10 ilijaNL

Same here :(

quentin-decre avatar Dec 02 '22 09:12 quentin-decre

SOLUTION: Do as @rikinsk mentions in #3066 . Add an explicit constraint, not just a unique index.

ALTER TABLE <table_name> ADD CONSTRAINT <constraint_name> UNIQUE (<column1>, <column2> );

erikhSSI avatar Dec 02 '22 19:12 erikhSSI

Sometimes you can't just alter the table...

ilijaNL avatar Dec 02 '22 22:12 ilijaNL

@erikhSSI even if this behaviour is a known limitation, there is definitely a schema bug.

carlpaten avatar Dec 06 '22 16:12 carlpaten

I added the constraint manually and reloaded metadata but now Hasura doesn't recognize it. This is really frustrating.

ChrisBurgdorff avatar Jul 14 '23 21:07 ChrisBurgdorff

I removed the index and was able to add the constraint manually, no issues with the metadata but this was frustrating to debug.

0x15f avatar Aug 02 '23 14:08 0x15f