graphile / crystal-pre-merge

Repository moved
https://github.com/graphile/crystal
39 stars 10 forks source link

Unique indexed column raise error "combination of attributes is not unique" #510

Closed timnghg closed 9 months ago

timnghg commented 9 months ago

Do you think if it is intended and unrelated to #474?

Masked GraphQL error (hash: 'Spwi2IARCeGK-TlghR5Q0EvBBtw', id: 'YVAYUTKYG2') 61 |         value: originalError.stack,
62 |         writable: true,
63 |         configurable: true,
64 |       });
65 |     } else if (Error.captureStackTrace != null) {
66 |       Error.captureStackTrace(this, GraphQLError);
          ^
GraphQLError: Attempted to call PgResource(Country).get({code}) at child field (TODO: which one?) but that combination of attributes is not unique (uniques: [{"isPrimary":true,"attributes":["id"],"extensions":{"tags":{}}}]). Did you mean to call .find() instead?
 path: "undefined"
      at new GraphQLError (~timnghg/proj1/node_modules/grafast/node_modules/graphql/error/GraphQLError.js:66:6)
      at Pe (~timnghg/proj1/node_modules/grafast/dist/index.js:238:739)
      at ~timnghg/proj1/node_modules/grafast/dist/index.js:238:207
      at compiledOutputPlan_object (:53:6)
      at compiledOutputPlan_array (:59:45)
      at compiledOutputPlan_object (:133:16)
      at compiledOutputPlan_object (:44:54)
      at ~timnghg/proj1/node_modules/grafast/dist/index.js:444:16405
      at x (~timnghg/proj1/node_modules/grafast/dist/index.js:444:64365)
      at A (~timnghg/proj1/node_modules/grafast/dist/index.js:444:71445)

image

benjie commented 9 months ago

A unique index is different from a unique constraint (The latter contains a unique index automatically). Indexes are generally optimisations, whereas constraints are something you declare about your data, so we only use constraints when determining uniqueness.

You can use the ALTER TABLE … ADD CONSTRAINT … UNIQUE USING INDEX … syntax to add a unique constraint using your existing unique index, which should be near-instant: https://www.postgresql.org/docs/current/sql-altertable.html