Update dgraph database using pydgraph

Hi,

Through ratel UI I am able to update databse by adding one more triple like this,
I run the below query under ‘Mutate’,

{
  set {
    _:name1 <name> "Aman" .
    _:name2 <name> "Basketball" .
    _:name1 <likes_for> _:name2 .
  }
}

Which gives a triple Aman - > likes_for → basketball.

How can I implement the same using the pydgraph. Do I need to use the “create_data” mentioned over this link?

If yes, how would I represent my data inside the functions?

Thanks

As specified in the pydgraph mutation docs, you can run mutations in pydgraph in either JSON format (as a dict) or NQuad format using Txn#mutate(set_obj=mutation_dict) or and Txn#mutate(set_nquads=nquads_str) respectively.

I’m not sure this syntax works. I tried it myself and the mutation did not succeed. Edit: I edited your original post to format your mutation in a code block to show everything (<name> got removed since it was treated as HTML tags that needed to be sanitized). Your mutation works as expected.

Thanks @dmai

I tried this and it worked,

ls = """

       _:name1 <name> "Aman".

       _:name2 <name> "Badminton" .

       _:name1 <likes_for> _:name2 .

       """

        # Run mutation.
        assigned = txn.mutate(set_nquads=ls)

well this work.

1 Like

That looks good :+1:.

hi
I tried this,however, the scheme update the predicate but all of them point to null

Hey @Linfei, since this post is already marked as solved, could you please open a new topic with details of all the steps you performed and the version of dgraph, pydgraph and/or ratel you are using?

1 Like