Skip to content

Support 1 to 1 associations at the schema level #2511

@camway

Description

@camway

Me and another member of the dgraph forum have been going back and forth on how exactly this should be done. Feel free to read

https://discuss.dgraph.io/t/return-single-object-instead-array-for-exactly-one-relation-between-node/2927/7

The solution proposed by bennyrio (the person I've been talking to in the forum) was elaborated on after creating this. I do not want to misrepresent his view on this so below is a direct link to his explanation:

https://discuss.dgraph.io/t/return-single-object-instead-array-for-exactly-one-relation-between-node/2927/8

The essence of my request is that I think we should change the schema so that uid associations are capable of being defined as 1 to 1. Unfortunately, my proposal will be a breaking change, but I think it will make uid predicates consistent with the other predicate types.

Currently a schema of this:

a: uid .
b: [string] .
c: int .

Exhibits these behaviors:

a is capable of returning 0 or more nodes
b is capable of returning 0 or more strings
c is capable of returning 0 or 1 integers

My proposal would look like this:

a: uid .
b: [string] .
c: int .
d: [uid] .

Exhibiting these behaviors:

a is capable of returning 0 or 1 nodes
b is capable of returning 0 or more strings
c is capable of returning 0 or 1 integers
d is capable of returning 0 or more nodes

This would also require that any mutation which would violate this 1 to 1 association should abort with an error.

This would also mean that existing schemas would have to be updated, but I believe this will make for consistency across all predicates types when defining a list vs a single item.

Metadata

Metadata

Assignees

Labels

breaking_changekind/enhancementSomething could be better.priority/P1Serious issue that requires eventual attention (can wait a bit)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions