Dgraph: Support 1 to 1 associations seems to fail in V1.1.0

Created on 27 Sep 2019  路  3Comments  路  Source: dgraph-io/dgraph

What version of Dgraph are you using?

Dgraph version   : v1.1.0
Dgraph SHA-256   : 7d4294a80f74692695467e2cf17f74648c18087ed7057d798f40e1d3a31d2095
Commit SHA-1     : ef7cdb28
Commit timestamp : 2019-09-04 00:12:51 -0700
Branch           : HEAD
Go version       : go1.12.7

Have you tried reproducing the issue with the latest release?

I think that's the latest one.

What is the hardware spec (RAM, OS)?

Ubuntu 16.04, 16GB, don't think it's relevant here.

Steps to reproduce the issue (command/config used to run Dgraph).

run dgraph 1.1.0
clone this branch of this repo:
https://github.com/uprtcl/js-uprtcl-server/tree/duplicated-heads

npm install
npm test

Expected behaviour and actual result.

I configured the Perspective type as having one property called head of type uid (not [uid]). Check src/db/schema.ts and /src/db/dgraph.service.ts/

Since this issue was closed and added to V1.1.0, I expect that every time I set the head of a perspective the value is overwritten instead of added to the previous one.

In the last console.log there should be one single head edge. Instead, there are two.

[DGRAPH] getPerspectiveHead {
      query: 'query {\n' +
        '      perspective(func: eq(xid, zb2wwruCksbXqsGYMVHZsFi6TcWWW7rNTnerJN2RBzE5ZecDU)) {\n' +
        '       head {\n' +
        '         xid\n' +
        '       }\n' +
        '      }\n' +
        '    }'
    } { perspective: [ { head: [Array] } ] } {
      head: [
        { xid: 'zb2wwzqMtQFTuvXWtQGQchSCzfAjE4GEWJby3wxbRTabjnTkb' },
        { xid: 'zb2wwyEFUzAAdf7ww8FqGVorGhwgf5fejhSmdVw1vd2aZuRuC' }
      ]
    }
areschema kinbug statuaccepted

All 3 comments

I think you've just defined the type schema. Dgraph won't check the type schema, you have to add in https://github.com/uprtcl/js-uprtcl-server/blob/05a19237417d7115e7457defe6d143e27a5cf8fb/src/db/schema.ts#L63 this:

head: uid .

Yeah, could you give us the result of running the following query?

schema {}

OK, I was able to reproduce the issue and I have a workaround for you.

The problem is that you didn't declare the head predicate in the Dgraph schema, you just created it as a field in the type Perspective.

This means Dgraph doesn't know about the head predicate at all! So when you create some data with a head predicate, Dgraph defaults to the [uid] type as it's supposed to.

So, in your case you should definitely add the following line to your schema:

<head>: uid .

This will enforce having the 1:1 relationship you wanted.

Additionally, if you want to avoid this behavior in the future and make sure no predicates are created without your permission, you should definitely protect the database by running your alphas with the --mutations=strict flag on.

$ dgraph alpha --help | grep mutations
      --mutations string            Set mutation mode to allow, disallow, or strict. (default "allow")

That will stop any mutations that don't follow the schema.

Finally, there's the question of whether the predicate should be created once we see it on a type.
I do think so, so I created #4085 to track it.

Thanks for the report!

Closing this issue now, feel free to reopen if there's anything else I missed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

djdoeslinux picture djdoeslinux  路  4Comments

fritzblue picture fritzblue  路  5Comments

pjebs picture pjebs  路  4Comments

marvin-hansen picture marvin-hansen  路  4Comments

xhochipe picture xhochipe  路  3Comments