How can i update parent node while inserting new child node?

Hello I’m just tarting to use dgraph and while trying to replicate something, i can’t make it work.

I will try to explain my problem below:

What I want to do

update parent node while inserting new child node

What I did

  1. Create schema like below :
type House{
    name: String
    people: [Person] @hasInverse(field: house)
}

type Person{
    name: String
    house: House
}

Using python client i added mutations:

  1. Adding house with people:
client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)

txn = client.txn()

p = {
       'House.name': 'House of Bob',
       'House.people' : [ {'Person.name' : "Alice"} , {'Person.name' : "Joey"}]
}

mu = pydgraph.Mutation(set_json=json.dumps(p).encode('utf8'))
txn.mutate(mu)
txn.commit()

After indexing House.name i can use following query to see house + people relations:

{
    q (func : eq(House.name, "House of Bob")) {
      House.house_name
      House.people
      {
				Person.name
      }
    }
}

i was able to see nodes with connections

  1. Adding person with house :
client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)

txn = client.txn()

            p = {
                'Person.name' : 'John',
                'Person.house' : {'House.name' : 'House of Bob'}
            }

mu = pydgraph.Mutation(set_json=json.dumps(p).encode('utf8'))
txn.mutate(mu)
txn.commit()

Once i try to add person like this, brand new “House of Bob” appears and i end up having 2 “House of Bob”

1 house with 2 people and 1 house with 1 person

I think i should be inserting uid of {‘House.name’ : ‘House of Bob’} to ‘Person.house’ like

‘Person.house’ : “uid”

But im not sure how to do it after reading https://dgraph.io/docs/graphql/schema/graph-links/.

Thanks in advance!

Dgraph metadata

dgraph version v21.03.0
pydgraph version 21.3.0

This should be

 p = {
                'Person.name' : 'John',
                'Person.house' : [{'uid' : '0x1'}]
            }

In Dgraph the relations are UID based, not value based.

Also, you are building your Schema using GraphQL Schema. DQL and GQL Schemas are completly different topics. You should not rely on a GQL schema and work with DQL. There are different features and approaches there.

If you still wanna sitck to GraphQL Schema, there are several points to take into consideration before continuing.

After @MichelDiz’s comment i changed my schema to DQL:

<house>: [uid] @reverse .
<name>: string @index(exact, term) @lang .
<people>: [uid] .
type <House> {
	name
	people
}
type <Person> {
	name
	house
}

my mutations:

           p = {
                'name': 'House of Bob',
                'people': [{"name" : "Alex"}]
            }

and

            p = {
                'name' : 'Tess',
                'house' : {'uid' : '0x94'}
            }

and while queriying used reverse edge

The reverse edge of anEdge is ~anEdge.

reference https://dgraph.io/docs//query-language/schema/#reverse-edges

query :

{
    q (func : eq(name, "House of Bob")) {
    name
    people : people
    {name}
	reverse : ~house
    {name}
  }
}

I was able to solve my issue. Response:

"data": {
    "q": [
      {
        "name": "House of Bob",
        "default": [
          {
            "name": "Alex"
          }
        ],
        "reverse": [
          {
            "name": "Tess"
          }
        ]
      }
    ]
  }