Update a node adding edges

Moved from GitHub pydgraph/100

Posted by ogonbat:

Hi All

first of all I’m quite new with dgraph and maybe my question is a bit stupid, so in that case, sorry to waste your time.

i have a schema:


    id: int @index(int) .
    name: string @index(term) .
    realname: string .
    aliases: [uid] @reverse @count .
    namevariations: [string] .
    affiliation: uid @reverse @count .
    address: string @index(exact) .
    number: string .
    email: string .
    type Aie {
      address: string
      number: string
      email: string
    type Artist {
        id: int
      name: string
      realname: string
      aliases: [Artist]
      namevariations: [string]
      affiliate: Aie


first of all i have added artists with a mutation and all worked well.
the second step was update the Artist type with aliases, to do that i have created a query:


 query_artist_by_id = """
            query artist($a: int) {
                 artist(func: eq(id, $a)) {


this is the python code


    path_xml = os.path.join(BASE_PATH, "uploader_db", 'xml', 'artists.xml')
    init_numb = 0
    list_artists = []
    artist = {}
    for event, element in etree.iterparse(path_xml, tag="artist"):
        # check if trhe artist have aliases
        aliases = element.find('aliases')
        members = element.find('members')
        if aliases is not None and members is None:
            # tag is an artist and have aliases
            id = element.find('id')
            # get the user from the id
            variable = {'$a': id.text}
            res = client.txn(read_only=True).query(query_artist_by_id, variables=variable)
            ppl = json.loads(res.json)
            aliases_array = []
            for artist_aliases in aliases.getchildren():
                variable = {'$a': artist_aliases.get('id')}
                res_alias = client.txn(read_only=True).query(query_artist_by_id, variables=variable)
                rpl = json.loads(res_alias.json)
                if len(rpl['artist']) > 0:
            if len(aliases_array) > 0:
                ppl['aliases'] = aliases_array
                txn = client.txn()
                    response = txn.mutate(set_obj=ppl)
                except Exception as e:
                    if isinstance(e, pydgraph.AbortedError):
                        raise e
                    aliases_array = []


in the python code i do that:

read the file with the content and iter all the results
i find into the dgraph database the Artist with the query below.
i find the Artist for every aliases that i have parsed and i update the main Artist object
and finaly i send the mutation

the problem is that after the update the Artist object with aliases loose the information like name, namevariations etc

i do something wrong?

thanks in advance

martinmr commented :

If you are updating the same object, you shouldn’t be seeing this. The fields not mentioned in the object should not be touched. Unless, you sent an object without a valid UID, in which case a new node with the attributes you mentioned is specified.

If the only thing you are changing are the aliases, your object should be something like.

uid: [same UID as the original object]
aliases: [list of aliases uids, not the entire object]

I would avoid including any fields in the object that you are not trying to modify. I think the problem is you are sending the entire object as aliases so it creates new objects instead of linking the existing artists. I am not entirely sure if what I am suggesting is allowed in the object format (it seems like that format is more geared towards creating objects, rather than modifying them) but it’s worth a try.

Otherwise you can use RDF triples, in which case your mutations become.

<uid_of_artist> <aliases> <uid_of_first_alias> .
<uid_of_artist> <aliases> <uid_of_second_alias> .