Return single object instead array for exactly one relation between node

I prefer dgraph to keep the “RDF spirit”

In a sense, just like RDF, dgraph is schema-less, CMIIW, schema in dgraph is just type safety compared to schema in SQL, we can have dgraph database without even having a schema, but it will be a bit verbose because we need to add RDF type in every mutation.

Even for uniqueness constraint, in dgraph, a programmer must handle it by their hand through transaction: Unique Indexes on predicates

So, to emulate your SQL unique constraint example in current dgraph implementation, just do it through transaction, for example when adding a new student node.

  • First we check if student with school is already exist in dgraph database, by doing this query:
{
    checking_query(func: uid(student_uid)) {
        name
        study_at @filter(uid(school_uid))  {
           name
        }
    }
}
  • if that query return a result, then abort the transaction, which mean it violating “a student must study in exactly one school” constraint, if not continue the transaction by adding new student data.

My proposal doesn’t related to uniqueness constraint, but query result representation, currently dgraph always return array for node-to-node predicate/relationship, it would nice if we have a query filter to tell dgraph to return single object on special case, like one-to-one relationship