The --upsertPredicate flag is ignored during JSON live upload

I’m running a local standalone dgraph instance using the latest docker container.

Then I upload a GraphQL schema using
❯ curl -X POST localhost:8080/admin/schema --data-binary '@schema.graphqls'

The schema looks like:

interface IdentifiedObject {
    id: ID!
    iri: String! @id @dgraph(pred: "xid")
    description: String 
    name: String 
}

type BaseVoltage implements IdentifiedObject   {
    id: ID!
    iri: String! @id @dgraph(pred: "xid")
    description: String  
    name: String  
    nominalVoltage: Voltage  
}

type ACLineSegment implements IdentifiedObject {
    id: ID!
    iri: String! @id @dgraph(pred: "xid")
    BaseVoltage: BaseVoltage 
    Terminals: [Terminal!] 
    description: String 
    name: String
}

Then I use live upload command
❯ docker exec dgraph dgraph live --alpha localhost:9080 --format=json -f /tmp/lines_500.json --upsertPredicate xid

The content of the json file looks like:

[
    {
        "dgraph.type": "ACLineSegment",
        "IdentifiedObject.iri": "urn:uuid:0043d68c-2d4c-4965-9c8a-e1fca6ecd178",
        "xid": "urn:uuid:0043d68c-2d4c-4965-9c8a-e1fca6ecd178",
        "IdentifiedObject.name": "110 kV C.D. Tamaya-Salar",
        "ACLineSegment.BaseVoltage": {
            "dgraph.type": "BaseVoltage",
            "IdentifiedObject.iri": "urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "xid": "urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "IdentifiedObject.name": "110.00 kV",
            "BaseVoltage.nominalVoltage": null
        },
    },
    {
        "dgraph.type": "ACLineSegment",
        "IdentifiedObject.iri": "urn:uuid:0084d6fc-e131-432d-92bb-1e0a12dc95db",
        "xid": "urn:uuid:0084d6fc-e131-432d-92bb-1e0a12dc95db",
        "IdentifiedObject.name": "EST.67 - EST.74 110KV C1",
        "ACLineSegment.BaseVoltage": {
            "dgraph.type": "BaseVoltage",
            "IdentifiedObject.iri": "urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "xid": "urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "IdentifiedObject.name": "110.00 kV",
            "BaseVoltage.nominalVoltage": null
        },
    },
]

Command executes successfully and I’m making a request:

{ 
  lines(func: type(BaseVoltage)) {
    uid
    xid
  }
}

It returns me 2 BaseVoltage’s instead of one which is my expectation.

It seems for me that the --upsertPredicate flag doesn’t work properly - please explain what am I doing wrong. The expected behaivour is having 2 different lines referencing a single voltage object.

Thank you.

I’ll reply to myself, so others will be able to find the solution.

After reading the code of the live updater, I figured how everything should be configured.

  1. Each object in the json must have dgraph.type specified
  2. If upsert is intended, you need to provide a uid for each object. The easiest way is provide it in a form of blank node _:<any string>
  3. If you want to have a separate unique global index by some field, you want to add @dgraph(pred: "<global id predicate name>") and you don’t want to specify this predicate on each object by yourself in json. You should also add a flag to the live uploader (--upsertPredicate <global id predicate name>), so your predicate will be queryable.

In my example I did:

Schema:

interface IdentifiedObject {
    id: ID!
    iri: String! @id @dgraph(pred: "xid")
    description: String 
    name: String 
}

type BaseVoltage implements IdentifiedObject   {
    id: ID!
    iri: String! @id @dgraph(pred: "xid")
    description: String  
    name: String  
    nominalVoltage: Voltage  
}

type ACLineSegment implements IdentifiedObject {
    id: ID!
    iri: String! @id @dgraph(pred: "xid")
    BaseVoltage: BaseVoltage 
    Terminals: [Terminal!] 
    description: String 
    name: String
}

Data:

[
    {
        "uid": "_:urn:uuid:0043d68c-2d4c-4965-9c8a-e1fca6ecd178",
        "dgraph.type": "ACLineSegment",
        "IdentifiedObject.iri": "urn:uuid:0043d68c-2d4c-4965-9c8a-e1fca6ecd178",
        "IdentifiedObject.name": "110 kV C.D. Tamaya-Salar",
        "ACLineSegment.BaseVoltage": {
            "uid": "_:urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "dgraph.type": "BaseVoltage",
            "IdentifiedObject.iri": "urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "IdentifiedObject.name": "110.00 kV",
            "BaseVoltage.nominalVoltage": null
        },
    },
    {
        "uid": "_:urn:uuid:0084d6fc-e131-432d-92bb-1e0a12dc95db",
        "dgraph.type": "ACLineSegment",
        "IdentifiedObject.iri": "urn:uuid:0084d6fc-e131-432d-92bb-1e0a12dc95db",
        "IdentifiedObject.name": "EST.67 - EST.74 110KV C1",
        "ACLineSegment.BaseVoltage": {
            "uid": "_:urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "dgraph.type": "BaseVoltage",
            "IdentifiedObject.iri": "urn:uuid:01bb6a99-59af-7681-acb7-5a157aa705aa",
            "IdentifiedObject.name": "110.00 kV",
            "BaseVoltage.nominalVoltage": null
        },
    },
]

Command:
❯ docker exec dgraph dgraph live --alpha localhost:9080 --format=json -f /tmp/data.json --upsertPredicate xid

After that my queries for BaseVoltage nodes return exactly one node.

1 Like

Thanks a lot for the help @Sergey_Babinskiy

1 Like

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