wileni
wileni

Reputation: 35

How to update nodes in Dgraph using Python

My schema is:

schema = """id: int @index(int) .
             question: string .
             answer: string .
             creation_time: int .
             interval_time: int .
             box_id: int .
             
             type Card {
                id
                question
                answer
                creation_time
                interval_time
                box_id
             }"""

Let's say I've inserted some nodes and I want to update the question of one card. How would I do that? I know there is a set function, but I don't know how to use it in python and couldn't find examples or anything else.

Upvotes: 0

Views: 560

Answers (1)

amaster
amaster

Reputation: 2163

There is a python client with example mutations found in the Dgraph docs:

https://dgraph.io/docs/clients/python/#running-a-mutation

nquads = """
  <0x2a> <question> "New Question Text Here..." . 
"""
mutation = txn.create_mutation(set_nquads=nquads)
request = txn.create_request(mutations=[mutation], commit_now=True)
txn.do_request(request)

Where 0x2a is the uid of the Card you want to update. If you don't have the uid, then you will need to follow the upsert pattern.

query = """
  { u as var(func: eq(id, 42)) @filter(type(Card)) }
"""
nquad = """
  uid(u) <question> "New Question Text Here..." . 
"""
mutation = txn.create_mutation(set_nquads=nquad)
request = txn.create_request(query=query, mutations=[mutation], commit_now=True)
txn.do_request(request)

Where 42 is the id of the Card you want to update

Upvotes: 1

Related Questions