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

Adding custom constraints to tables

Open frandiox opened this issue 5 years ago • 2 comments

According to the docs, Hasura supports Check Constraints for tables but not other types like unique or exclusion.

I've tried adding an exclusion constraint like this:

ALTER TABLE <table> ADD CONSTRAINT <my_constraint> EXCLUDE (<column_a> WITH =) WHERE (<column_b> = <value>);

It works when I add rows manually but the GraphQL API is not updated properly. The <table>_constraints enum does not include <my_constraint> value, so it cannot be used in insert_table_one(..., on_conflict: { constraint: ... }).

I've checked the existing constraints in information_schema.table_constraints but it does not list <my_constraint>. It is listed, however, in pg_catalog.pg_constraint.

Any known workaround? Thanks!

-- Edit

Something similar happens when adding a custom index:

CREATE UNIQUE INDEX <my_index> ON <table> (<column_a>) WHERE <column_b> = <value>;

With this, it shows <my_index> in <table>_constraint enum for on_conflict, but it returns this error when doing a mutation:

    {
      "extensions": {
        "path": "$.selectionSet.insert_<table>_one.args.object[0]",
        "code": "constraint-error"
      },
      "message": "constraint \"<my_index>\" for table \"<table>\" does not exist"
    }

frandiox avatar Jul 01 '20 09:07 frandiox

same problem here

dnavamosler avatar Nov 06 '21 17:11 dnavamosler

Same issue, was there an update in the meantime?

ThomasKoscheck avatar Jan 21 '25 21:01 ThomasKoscheck