Define a unique index in EdgeDB

193 views Asked by At

How do you define a unique index in EdgeDB?

For simple properties you can add constraint exclusive which implicitly creates a unique index.

required property name -> str {
    constraint exclusive;
}

However you can't add constraint exclusive to an explicitly defined index. How do you mark such an index as unique?

1

There are 1 answers

0
CodesInChaos On BEST ANSWER

You can add constraint exclusive on (...) to the containing type, where ... is an arbitrary scalar expression. To ensure the uniqueness of multiple properties, it should be a tuple (x, y):

type User {
    property first_name -> str;
    property last_name -> str;
    constraint exclusive on ((.first_name, .last_name));
}

An exclusive constraint implicitly creates an index.

source