Conditional Upsert: Adding intermediary node

I am working on building a database of events that are structured as, Actor -> Action -> Object. There will be many actions stemming from the Actor node and connected to Objects (sometimes the same object). I am working on a conditional upsert so when an action is added if the object it acts on already exists it will connect to the existing object. When the object does not already exist, a new one will be made.

I am using the pydgraph client currently and my code is below. The client will not run because of an error that Object_ID is not used, however it is used in the conditional statement. Is this a bug or are variables not supposed to be used in conditionals?


        # Query for Actor_uid, Object_uid, and Object_ID
        query = """{
            var(func: eq(actorID, %s)){
            Actor_uid as uid
            action {
                acts_on {
                    Object_uid as uid
                    Object_ID as objectID
        }""" % actorID
        # Conditional to check if the Objects have the same ID
        cond = "@if(eq(Object_ID, %s))" % objectID
        # N_quad to build ActionNode
        n_quad = """
            _:ActionNode1 <type> %s .
            uid(Actor_uid) <action> _:ActionNode1 .
            _:ActionNode1 <acts_on> uid(Object_uid) .
        """ % type
        mutation = txn.create_mutation(cond=cond, set_nquads=n_quad)
        request = txn.create_request(mutations=[mutation], query=query, commit_now=True)


You should change it to

cond = "@if(eq(val(Object_ID), %s))"
1 Like

Thank you!