0

I'm having trouble building a hasura graphql query that inserts a row into a table that has multiple foreign key relationships.

I have three tables, "Users", "Groups", and "UserGroups". The "UserGroups" table has foreign key relationships on columns groupId, and userId. I used Hasura's UI to track the relationships that it had suggested based on the two foreign keys.

I can create new UserGroups by connecting to the database directly but when I try to create a new UserGroup through a graphql query I get a foreign key violation response.

{
  "errors": [
    {
      "extensions": {
        "path": "$.selectionSet.insert_user_group.args.objects",
        "code": "constraint-violation"
      },
      "message": "Foreign key violation. insert or update on table \"user_group\" violates foreign key constraint \"user_group_userId_fkey\""
    }
  ]
}

Here is the mutation I'm trying to use:

mutation insert_user_group {
  insert_user_group(objects: [{
    userId: 1,
    groupId: 1,
  }]) {
    affected_rows
  }
}

I was only able to find documentation regarding querying tables with one-to-many relationships but nothing showing how to construct an insert mutation.

Michael Fox
  • 491
  • 5
  • 15
  • 2
    The foreign key constraint violation implies that userId with value 1 doesn't exist on `Users` table. Are you sure the the row exists in the `Users` table before doing the above mutation? – praveenweb Nov 18 '19 at 03:33
  • Yea, I can use the same values and insert the row successfully when using the data tab. It's only when using the GraphIQL view where the error is occurring. – Michael Fox Nov 18 '19 at 16:57

1 Answers1

0

Turns out it was failing because I wasn't using strings in the query for the id values. They are BigInt columns but the queries required strings for the insert to work correctly.

Thank you @praveenweb for your comment.

Michael Fox
  • 491
  • 5
  • 15