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.

1 Answer 1

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

3
  • 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
    Commented Jun 23, 2021 at 6:03
  • @dbuser, I added a full example
    – amaster
    Commented Jun 23, 2021 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
    Commented Jun 24, 2021 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.