# Upsert with multiple UIDs

**URL:** <https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223>\
**Category:** Users\
**Tags:** mutation, java-client\
**Created:** [October 12, 2019, 12:02am UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223 "2019-10-12T00:02:07Z")\
**Posts on this page:** 9\
**Page:** 3

<div class="post-metadata">

**Author:** ![devinbost](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/devinbost/32/2126_2.png) [@devinbost](https://discuss.dgraph.io/u/devinbost)\
**Post date:** [October 16, 2019, 6:50pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/43 "2019-10-16T18:50:02Z")

</div>

I’m not sure how to setup a Gist for a Dgraph query, but you can reproduce it with these steps:

First, run this alter on the schema:

```auto
type Products { 
    products: [Product] 
} 
type Product { 
    productId: string 
    options: [Option] 
} 
type Option { 
    optionId: string 
    color: string 
}
<collectionId>: int @index(int) .
<color>: string .
<optionId>: int @index(int) .
<options>: [uid] .
<productId>: int @index(int) .
<products>: [uid] .

```

Then, run this mutate:

```auto
{
  "set":[ {
    "uid": "_:products",
    "dgraph.type": "Products",
    "collectionId": 1,
    "products": [
      {
        "dgraph.type": "Product",
        "uid": "_:product",
        "productId": 19610626,
        "options": [
          {
            "dgraph.type": "Option",
            "uid": "_:option",
            "optionId": 32661491,
            "color": "red"
          }
        ]
      }
    ]
}]
}

```

Then, run this Java code:

```auto
String query = "{\n" +
            " getVals(func: has(products)) {\n" +
            " productsUid as uid\n" +
            " products @filter(eq(productId, 19610626)) {\n" +
            " productUid as uid\n" +
            " options @filter(eq(optionId, 32661491)) {\n" +
            " optionUid as uid\n" +
            " }\n" +
            " }\n" +
            " }\n" +
            "}";

DgraphProto.Mutation mu =
            DgraphProto.Mutation.newBuilder()
                    .setSetNquads(ByteString.copyFromUtf8("uid(productsUid) <products> uid(productUid) ."))
                    .setSetNquads(ByteString.copyFromUtf8("uid(productsUid) <dgraph.type> \"Products\" ."))
                    .setSetNquads(ByteString.copyFromUtf8("uid(productUid) <productId> \"19610626\" ."))
                    .setSetNquads(ByteString.copyFromUtf8("uid(productUid) <options> uid(optionUid) ."))
                    .setSetNquads(ByteString.copyFromUtf8("uid(productUid) <dgraph.type> \"Product\" ."))
                    .setSetNquads(ByteString.copyFromUtf8("uid(optionUid) <color> \"blue\" ."))
                    .setSetNquads(ByteString.copyFromUtf8("uid(optionUid) <dgraph.type> \"Option\" ."))
                    .setSetNquads(ByteString.copyFromUtf8("uid(optionUid) <optionId> \"32661491\" ."))
                    .build();
    Map uidsMap;
    try(DgraphConnection dgraphConnection = DGraphQueryHelper.createDgraphClient(false, context)){
        Transaction txn = dgraphConnection.getDgraphClient().newTransaction();
        try{
            DgraphProto.Request request = DgraphProto.Request.newBuilder()
                    .setQuery(query)
                    .addMutations(mu)
                    .setCommitNow(true)
                    .build();
            DgraphProto.Response res = txn.doRequest(request);
            uidsMap = res.getUidsMap();
            String exactOutput = res.getJson().toStringUtf8();
        } catch (Exception e){
            throw e;
        }
        finally {
            txn.discard();
        }

    } catch (Exception e){
        logger.error("The message on the exception is: " + e.getMessage());
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        logger.error("Showing stack trace: " + sw.toString());
        throw e;
    }

```

The spacing got messed up a little when I pasted the code, but I don’t think it has any syntax errors.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [October 16, 2019, 7:00pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/44 "2019-10-16T19:00:30Z")

</div>

I meant a github gist like so [upsert test · GitHub](https://gist.github.com/MichelDiz/4e1fee3be8f7c95daeab8e2aff291751)  
So, this upsert block is working great. No errors. It might mean that some typo in your Java Code is happening. As I don’t know Java I can’t tell what is it.

---

<div class="post-metadata">

**Author:** ![devinbost](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/devinbost/32/2126_2.png) [@devinbost](https://discuss.dgraph.io/u/devinbost)\
**Post date:** [October 16, 2019, 7:11pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/45 "2019-10-16T19:11:11Z")

</div>

Could there be a bug in the Java Dgraph client?

---

<div class="post-metadata">

**Author:** ![devinbost](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/devinbost/32/2126_2.png) [@devinbost](https://discuss.dgraph.io/u/devinbost)\
**Post date:** [October 16, 2019, 7:26pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/46 "2019-10-16T19:26:43Z")

</div>

I just noticed that there was more information in the exception that was thrown. It stated:

> io.grpc.StatusRuntimeException: UNKNOWN: while parsing query: . . . Some variables are defined but not used  
> Defined:[optionUid productUid productsUid]  
> Used:[optionUid]

Why would it be saying that only `optionUid` was used? That seems odd to me.  
I wonder if @amanmangal would have more ideas about this because he’s one of the Java client maintainers.

---

<div class="post-metadata">

**Author:** ![dmai](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/dmai/32/1254_2.png) [@dmai](https://discuss.dgraph.io/u/dmai)\
**Post date:** [October 16, 2019, 7:39pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/47 "2019-10-16T19:39:54Z")

</div>

You should only call `.setSetNquads` once in the Mutation builder. As you currently have it, only the last setSetNquads call is ultimately applied, which is why the error says only optionUid is used.

---

<div class="post-metadata">

**Author:** ![devinbost](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/devinbost/32/2126_2.png) [@devinbost](https://discuss.dgraph.io/u/devinbost)\
**Post date:** [October 16, 2019, 8:01pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/48 "2019-10-16T20:01:24Z")

</div>

That’s the issue! Thanks for the help!

Now, is there a way for me to be able to get the UIDs back out of the upsert? Or, will I need to run a second query to do that?  
(I’m referring to this line: `uidsMap = res.getUidsMap()`, which is currently empty.)

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [October 16, 2019, 8:04pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/49 "2019-10-16T20:04:30Z")

</div>

> [@devinbost](#):
>
> Now, is there a way for me to be able to get the UIDs back out of the upsert? Or, will I need to run a second query to do that?  
> (I’m referring to this line: `uidsMap = res.getUidsMap()` , which is currently empty.)

it is empty cuz, for now, there’s no response for existent entities. Only returns UIDs when you create new entities/nodes.  
[https://github.com/dgraph-io/dgraph/issues/4048](https://github.com/dgraph-io/dgraph/issues/4048)

---

<div class="post-metadata">

**Author:** ![devinbost](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/devinbost/32/2126_2.png) [@devinbost](https://discuss.dgraph.io/u/devinbost)\
**Post date:** [October 16, 2019, 8:08pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/50 "2019-10-16T20:08:14Z")

</div>

You guys are awesome. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [November 15, 2019, 8:08pm UTC](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223/51 "2019-11-15T20:08:23Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

[Previous page](https://discuss.dgraph.io/t/upsert-with-multiple-uids/5223.md?page=2)
