Skip to content

Update a node adding edges #100

@ogonbat

Description

@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)) {
                    uid
                    id
                   name
                   realname
                   aliases
                   namevariations
                   affiliation
              }
}
"""

`

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():
                #print(artist_aliases.get('id'))
                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:
                    aliases_array.append(rpl['artist'][0])
            if len(aliases_array) > 0:
                ppl['aliases'] = aliases_array
                txn = client.txn()
                try:
                    response = txn.mutate(set_obj=ppl)
                    txn.commit()
                except Exception as e:
                    if isinstance(e, pydgraph.AbortedError):
                        break
                    else:
                        raise e
                        break
                finally:
                    txn.discard()
                    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions