在嵌套对象上具有冲突约束的Upsert Graphql Mutation

时间:2020-07-16 14:16:39

标签: graphql mutation hasura

我正在尝试在单个突变中进行一次upsert。这里有两个表Users表[id,isVerified,type]和Customer表[id,name,deviceToken]在这里,Customers.id是Users.Id的外键。
以下是突变-

 MyMutation {
  insert_Users(objects: [{isVerified: false, name: "+9100000000", type: "customer", 
    Customers: {data: {deviceToken: "TestToken001"}}}],
      on_conflict: {
            constraint: Users_name_key,
            update_columns: [isVerified]
          }) {
    affected_rows
    returning {
      Customers{
        deviceToken
      }
  }
}
} ```

//But when I run this, I get the exception 

{
  "errors": [
    {
      "extensions": {
        "path": "$.selectionSet.insert_Users.args.objects[0].Customers.data",
        "code": "constraint-violation"
      },
      "message": "Uniqueness violation. duplicate key value violates unique constraint \"Customers_pkey\""
    }
  ]
}

这似乎是因为我没有在嵌套的客户对象上设置冲突约束。如何为嵌套对象添加冲突约束?

1 个答案:

答案 0 :(得分:1)

您还需要在嵌套数据内添加约束对象。 像这样:

MyMutation {
  insert_Users(objects: [{isVerified: false, name: "+9100000000", type: "customer", 
    Customers: {
       data: {deviceToken: "TestToken001"}, 
       on_conflict: {
            constraint: Customers_pkey,
            update_columns: [deviceToken]
          }         
     }}],
      on_conflict: {
            constraint: Users_name_key,
            update_columns: [isVerified]
          }) {
    affected_rows
    returning {
      Customers{
        deviceToken
      }
  }
}
}