简体   繁体   English

为具有一对多外键关系的 object 构建 hasura-graphql 插入突变查询

[英]Structuring a hasura-graphql insert mutation query for an object with a one-to-many foreign key relationship

I'm having trouble building a hasura graphql query that inserts a row into a table that has multiple foreign key relationships.我无法构建一个 hasura graphql 查询,该查询将一行插入到具有多个外键关系的表中。

I have three tables, "Users", "Groups", and "UserGroups".我有三个表,“用户”、“组”和“用户组”。 The "UserGroups" table has foreign key relationships on columns groupId, and userId. “UserGroups”表在 groupId 和 userId 列上有外键关系。 I used Hasura's UI to track the relationships that it had suggested based on the two foreign keys.我使用 Hasura 的 UI 来跟踪它基于两个外键建议的关系。

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.我可以通过直接连接到数据库来创建新的用户组,但是当我尝试通过 graphql 查询创建新的用户组时,我得到一个外键违规响应。

{
  "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.我只能找到有关查询具有一对多关系的表的文档,但没有显示如何构造插入突变。

Turns out it was failing because I wasn't using strings in the query for the id values.事实证明它失败了,因为我没有在查询中使用字符串来获取 id 值。 They are BigInt columns but the queries required strings for the insert to work correctly.它们是 BigInt 列,但查询需要字符串才能使插入正常工作。

Thank you @praveenweb for your comment.感谢@praveenweb 的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM