Edgedb Exclusive Constraint
[tr] Türkçe Oku 2023-04-12
The term exclusive constraint
in EdgeDB refers to a type of constraint that ensures a specific property is unique or within a certain range.
For example, you can create an exclusive constraint in a User
object that ensures the email
property is unique, thus ensuring that each user is only saved once.
In the following example, we are defining an exclusive constraint that ensures uniqueness in the email property:
type User {
required property id -> uuid;
required property name -> str;
exclusive constraint unique_email on (.email);
property email -> str;
}
In this example, we defined a “unique_email” exclusive constraint for the User object, which ensures uniqueness in the email property. The on keyword is used to specify which property should be unique. In this case, we specified the email property using .email.
Constraints can include multiple properties and enforce uniqueness on combinations of these properties. For example, if you want to enforce uniqueness on the combination of email and phone properties for a User object, you can define a unique constraint as shown in the following example:
type User {
required property id -> uuid;
required property name -> str;
exclusive constraint unique_email_phone on (.email, .phone);
property email -> str;
property phone -> str;
}
In this example, we defined a “unique_email_phone” exclusive constraint for the User object. This constraint ensures uniqueness in the combination of the email and phone properties.