0

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.

wileni
  • 25
  • 7

1 Answers1

1

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

amaster
  • 1,915
  • 5
  • 25
  • 51
  • I read it but it doesn't mention updates of nodes and the part about mutations doesn't help me to understand how I could do it. – wileni Jun 23 '21 at 06:03
  • @dbuser, I added a full example – amaster Jun 23 '21 at 11:50
  • When I do it like in your code, I get an error: "TransactionError('Transaction has already been committed or discarded')". However, when I set commit_now to False I get the expected results. Thanks for your help! – wileni Jun 24 '21 at 05:39