The DEFINE FIELD statement allows you to instantiate a named field on a table, enabling you to set the field's data type, set a default value, apply assertions to protect data consistency, and set permissions specifying what operations can be performed on the field.
Requirements
You must be authenticated as a root owner or editor, namespace owner or editor, or database owner or editor before you can use the
DEFINE FIELDstatement.You must select your namespace and database before you can use the
DEFINE FIELDstatement.
Statement syntax
Example usage
The following expression shows the simplest way to use the DEFINE FIELD statement.
The fields of an object and the items in an array can be defined individually using the . operator for objects, or the indexing operator for arrays.
Non-unicode fields can be defined and set using backticks where necessary. Be sure that any periods to indicate nested fields are not inside the backticks, as anything enclosed in backticks will be treated as a literal string.
As the output shows, the . enclosed inside backticks in the last field results in a single non-nested field name that includes the period, while the one immediately preceding it is nested.
Defining data types
The DEFINE FIELD statement allows you to set the data type of a field. For a full list of supported data types, see Data types.
From version v2.2.0, when defining nested fields, where both the parent and the nested fields have types defined, it is no longer possible to have mismatching types, to prevent any impossible type issues once the schema is defined.
For example, the following will fail:
The above will fail with the following error:
Simple data types
A | vertical bar can be used to allow a field to be one of a set of types. The following example shows a field that can be a UUID or an int, perhaps for user records that have varying data due to two diffent legacy ID types.
Array type
You can also set a field to have the array data type. The array data type can be used to store a list of values. You can also set the data type of the array's contents, as well as the required number of items that it must hold.
Making a field optional
You can make a field optional by wrapping the inner type in an option, which allows you to store NONE values in the field.
The example below shows how to define a field user on a POST table. The field is of type record. This means that the field can store a record<user> or NONE.
Flexible data types
Flexible types allow you to have SCHEMALESS functionality on a SCHEMAFULL table. This is necessary for working with nested object types that need to be able to accept fields that have not yet been defined.
Taking the following CREATE statement:
Without FLEXIBLE, the metadata field will effectively be a SCHEMAFULL object with only a single defined field.
In versions of SurrealDB before 3.0, the result of the above statement was a record in which the metadata field was only able to populate the user_id field.
As of version 3.0, the statement now returns an error upon finding the first field that was not defined in the schema.
With FLEXIBLE, the output will be as expected as the schema now allows any sort of object to be a field on the user table, as long as values for name and metadata.user_id are present.
Using the DEFAULT clause to set a default value
You can set a default value for a field using the DEFAULT clause. The default value will be used if no value is provided for the field.
Using the DEFAULT and ALWAYS clauses
In addition to the DEFAULT clause, you can use the DEFAULT ALWAYS clause to set a default value for a field. The ALWAYS keyword indicates that the DEFAULT clause is used not only on CREATE, but also on UPDATE if the value is empty (NONE).
With the above definition, the primary field will be set to 123.456 when a new product is created without a value for the primary field or with a value of NONE, and when an existing product is updated if the value is specified the result will be the new value.
In the case of NULL or a mismatching type, an error will be returned.
On the other hand, if a valid number is provided during creation or update, that number will be used instead of the default value. In this case, 123.456.
Using the VALUE clause to set a field's value
The VALUE clause differs from DEFAULT in that a default value is calculated if no other is indicated, otherwise accepting the value given in a query.
A VALUE clause, on the other hand, will ignore attempts to set the field to any other value.
As the example above shows, a VALUE clause sets the value every time a record is modified (created or updated). However, the value will not be recalculated in a SELECT statement, which simply accesses the current set value.
To create a field that is calculated each time it is accessed, a computed field can be used.
Altering a passed value
You can alter a passed value using the VALUE clause. This is useful for altering the value of a field before it is stored in the database.
In the example below, the VALUE clause is used to ensure that the email address is always stored in lowercase characters by using the string::lowercase function.
Asserting rules on fields
You can take your field definitions even further by using asserts. Assert can be used to ensure that your data remains consistent. For example you can use asserts to ensure that a field is always a valid email address, or that a number is always positive.
As the ASSERT clause expects an expression that returns a boolean, an assertion with a custom message can be manually created by returning true in one case and using a THROW clause otherwise.
Making a field READONLY
The READONLY clause can be used to prevent any updates to a field. This is useful for fields that are automatically updated by the system. To make a field READONLY, add the READONLY clause to the DEFINE FIELD statement. As seen in the example below, the created field is set to READONLY.
Using IF NOT EXISTS clause
The IF NOT EXISTS clause can be used to define a field only if it does not already exist. You should use the IF NOT EXISTS clause when defining a field in SurrealDB if you want to ensure that the field is only created if it does not already exist. If the field already exists, the DEFINE FIELD statement will return an error.
It's particularly useful when you want to safely attempt to define a field without manually checking its existence first.
On the other hand, you should not use the IF NOT EXISTS clause when you want to ensure that the field definition is updated regardless of whether it already exists. In such cases, you might prefer using the OVERWRITE clause, which allows you to define a field and overwrite an existing one if it already exists, ensuring that the latest version of the definition is always in use
Using OVERWRITE clause
The OVERWRITE clause can be used to define a field and overwrite an existing one if it already exists. You should use the OVERWRITE clause when you want to modify an existing field definition. If the field already exists, the DEFINE FIELD statement will overwrite the existing definition with the new one.
Setting permissions on fields
By default, the permissions on a field will be set to FULL unless otherwise specified.
You can set permissions on fields to control who can perform operations on them using the PERMISSIONS clause. The PERMISSIONS clause can be used to set permissions for SELECT, CREATE, and UPDATE operations. The DELETE operation only relates to records and, as such, is not available for fields.
Array with allowed values
By using an Access Control List as an example we can show how we can restrict what values can be stored in an array. In this example we are using an array to store the permissions for a user on a resource. The permissions are restricted to a specific set of values.
Using regex to validate a string
You can use the ASSERT clause to apply a regular expression to a field to ensure that it matches a specific pattern. In the example below, the ASSERT clause is used to ensure that the countrycode field is always a valid ISO-3166 country code.
Interacting with other fields of the same record
While a DEFINE TABLE statement represents a template for any subsequent records to be created, a DEFINE FIELD statement pertains to concrete field data of a record. As such, a DEFINE FIELD statement gives access to the record's other fields through their names, as well as the current field through the $value parameter.
The $this parameter gives access to the entire record on which a field is defined.
Order of operations when setting a field's value
As DEFINE FIELD statements are computed in alphabetical order, be sure to keep this in mind when using fields that rely on the values of others.
The following example is identical to the above except that full_name has been chosen for the previous field name. The full_name field will be calculated after first_name, but before last_name.
A good rule of thumb is to organise your DEFINE FIELD statements in alphabetical order so that the field definitions show up in the same order as that in which they are computed.
Defining a literal on a field
A field can also be defined as a literal type, by specifying one or more possible values and/or permitted types.
One more example of a literal containing settings for a full text search filter:
Defining a TYPE for the id field
The DEFINE FIELD statement can be defined for the id field to specify the acceptable type of ID.
Complex IDs can be specified as well.
ASSERT and DEFAULT on id
ASSERT on the id field is evaluated like any other field assertion. Inside the assertion, $value is the whole record id; use id.id() (or record::id($value)) to inspect the key portion. Assertions run on create for generated, default-supplied, and explicitly supplied ids, and are skipped on update and under OPTION IMPORT.
DEFAULT supplies the record id when none is given in CREATE or INSERT, evaluated in the session context and coerced to the declared type. An explicit id in the statement always wins. DEFAULT ALWAYS is not allowed on id.
VALUE, REFERENCE, COMPUTED, READONLY, FLEXIBLE, and non-key TYPE clauses are forbidden on id, a restriction which apple to ALTER FIELD statements as well.
Defining a reference
A field that is a record link (type record, option<record>, array<record<person>>, and so on) can be defined as a REFERENCE. If this clause is used, any linked to record will be able to define a field of its own of type references which will be aware of the incoming links.
For more information, see the page in the datamodel section on references.