Unique constraints definition
Unique constraints are defined on the Collection document definition in the constraints field. This field takes an array of unique constraints and has the following form:
constraints: [
{
unique: [
"<fieldName>", ...
],
status: "<status>",
},
...
]
When one or more fields are part of a unique constraint, multiple documents cannot have the same combination of values for the constrained fields.
Constraints aren’t applied retroactively to existing documents but are applied only when a new document is created or an existing document is updated.
You can declare any number of constraints that define which Document
field values must be unique in a Collection. A unique constraint can
define one or more fields that must be unique in combination.
For example, if firstName
and lastName
fields are included in a unique
definition, the first and last name combination must be unique in the
collection.
If you create a unique constraint over more than one field, the absence of
one or more fields with the presence of other fields in the unique constraint
is a combination that the constraint is enforced over. In the firstName
/
lastName
example, if you create a user with only a lastName
, you can’t
create another user with only that lastName
, but you could if you add a
firstName
.
Constraints definition fields
Property | Type | Description |
---|---|---|
|
Array of document field names that are constrained to unique values. All defined fields are considered when validating create and update requests against constrained fields. |
|
|
(read-only) Constraint status: |
Examples
Basic unique constraints example
-
Given a collection definition:
{ name: "Hello", coll: Collection, ts: Time("2023-03-28T20:28:05.490Z"), indexes: {}, constraints: [], }
-
Create a unique constraint:+
{ name: "Hello", coll: Collection, ts: Time("2023-03-28T20:28:05.490Z"), indexes: {}, constraints: [ { unique: [ "what", ], status: "active", }, ], }
-
Create a document and populate it with the constrained field. The document is created successfully:
{ id: "360475803255832609", coll: Hello, ts: Time("2023-03-28T20:29:25.260Z"), what: "greeting", }
-
Create another document and try to populate it with the same value for the constrained field. The create fails because the constrained field already has the same value:
Error: constraint_failure Failed unique constraint. error: Failed unique constraint. constraint failures: what: Failed unique constraint at *query*:1:13 | 1 | Hello.create({ what: "greeting" }) | ^^^^^^^^^^^^^^^^^^^^^^ |
Compound and multiple unique constraints example
This example shows how to declare multiple unique constraints and constraints on a combination of values.
-
Given documents that have the shape of the following example:
{ id: "372518393394233409", coll: Customer, ts: Time("2023-08-08T18:41:14.200Z"), name: { first: "Kilgore", last: "Trout" }, username: "ktrout" }
-
Define two unique constraints, with one of the constraints requiring that the first and last names in combination are unique:
{ name: "Customer", coll: Collection, ts: Time("2023-08-08T20:08:36.240Z"), constraints: [ { unique: [ "username" ], status: "active" }, { unique: [ "name.first", "name.last" ], status: "active" } ], history_days: 0, indexes: {} }
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!