Constraint definitions

Fauna supports the following kinds of constraints:

Constraints are defined in the constraints field of the Collection document definition:

constraints: [
  {
    unique: [
      "<fieldName>", ...
    ], ...
    status: "<status>",
  },
  { check:
    { name: <constraintName>, body: <predicate> }
  }, ...
]

Unique constraint

Define unique constraints on select document fields to ensure that the values of the given fields are unique in the collection. Multiple unique constraints can be defined

Set of unique constraint definitions:

{
    unique: [
      "<fieldName>",
      . . .
    ],
    status: <constraintStatus>
}

Property Type Description

unique

List of document fields that are constrained to unique values. The fieldName value is evaluated when validating create and update requests.

You can define a constraint on a field that has the mva flag set. See Values for a description of the multivalued attribute flag (MVA).

status

(read-only) Constraint status:
active = Constraint is active, and a document can’t be added that conflicts with another document in the collection.
pending = Constraint isn’t yet active. If a constraint is added to a large collection, it takes time to account for existing documents, after which status transitions to active.

Unique constraints are implemented with a special index internal to Fauna. The time needed to build the index depends on the size of the collection. During that time the constraint status is pending. Also, as building an normal index consumes compute and read operations, building the index that supports the unique constraint also consume compute and read operations.

Unique constraints are applied when a new document is created or when an existing document is updated. Constraints aren’t applied retroactively to existing documents.

See collection.unique for more information.

Check constraint

A check constraint is a user-defined rule that constrain the values of a document field. Check constraints are implemented as predicates that control whether a document is written to a collection by validating that the value of a field is in an allowed set of values or range.

Check constraints are subject to the same memory and timeout limits as queries without check constraints.

Multiple check constraints can be defined

Set of check constraint definitions:

{
  check: {
    name: <constraintName>,
    body: (predicateBody: (<doc>: Document) => Boolean | Null)
  }
},
. . .

Property Type Required Description

name

String

Yes

Check constraint name. The constraintName is used to identify the constraint in error messages and must be unique for the collection.

body

Function

Yes

An anonymous, read-only FQL function that evaluates to true, false, or null. If false or null, Fauna rejects the write.

The function is passed one argument: an object containing the document to write.

The function runs using the built-in server role’s privileges.

Check constraints can read, call UDFs, and use computed fields but can’t write. A constraint that tries to write results in an evaluation error.

When a document in a collection is created, updated, or replaced, all check constraints are evaluated by passing the document to the check constraint.

See collection.check for more information.

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!