Skip to content

Statements

/

ALTER

ALTER TYPE

The ALTER TYPE statement changes the values of a union of strings declared with DEFINE TYPE, such as DEFINE TYPE priority AS 'low' | 'medium' | 'high'. It can add a value at any position in the order, and rename a value. A rename also rewrites the records that hold the old value.

SurrealQL Syntax
ALTER TYPE [ IF EXISTS ] @name
[
    ADD VALUE [ IF NOT EXISTS ] @value [ BEFORE @value | AFTER @value ] |
    RENAME VALUE @value TO @value
] ...

A statement takes one or more ADD VALUE and RENAME VALUE clauses, applied in the order they are written. If one clause fails, the statement changes nothing. Without IF EXISTS, a statement on a type that does not exist fails with The type 'name' does not exist. A type that is not a union of strings is refused with The type 'name' is not a union of string labels, so it has no values to alter. To change the checks, default, comment or permissions of a type, define it again with OVERWRITE, which checks the values that records already hold. See Changing a type that is in use.

ADD VALUE places a new value at the end of the order, or directly before or after an existing value. Adding a value does not change any stored record.

DEFINE TYPE priority AS 'low' | 'medium' | 'high';

ALTER TYPE priority ADD VALUE 'urgent';
ALTER TYPE priority ADD VALUE 'trivial' BEFORE 'low';
ALTER TYPE priority ADD VALUE 'normal' AFTER 'medium';

enum::values('priority');
Output
['trivial', 'low', 'medium', 'normal', 'high', 'urgent']

A value that the type already lists is refused, unless the clause has IF NOT EXISTS. A position that names a value the type does not list is refused as well.

-- Setup from the example above
DEFINE TYPE priority AS 'low' | 'medium' | 'high';
ALTER TYPE priority ADD VALUE 'urgent';
ALTER TYPE priority ADD VALUE 'trivial' BEFORE 'low';
ALTER TYPE priority ADD VALUE 'normal' AFTER 'medium';

ALTER TYPE priority ADD VALUE 'urgent';
//- "The type 'priority' already has the value 'urgent'"

ALTER TYPE priority ADD VALUE IF NOT EXISTS 'urgent';
//- NONE

ALTER TYPE priority ADD VALUE 'blocker' AFTER 'critical';
//- "The type 'priority' has no value 'critical'; its values are 'trivial', 'low', 'medium', 'normal', 'high', 'urgent'"

The clauses of one statement apply in order, so a later clause can refer to a value that an earlier one added or renamed.

-- Setup from the example above
DEFINE TYPE priority AS 'low' | 'medium' | 'high';
ALTER TYPE priority ADD VALUE 'urgent';
ALTER TYPE priority ADD VALUE 'trivial' BEFORE 'low';
ALTER TYPE priority ADD VALUE 'normal' AFTER 'medium';

ALTER TYPE priority
	ADD VALUE 'blocker' AFTER 'urgent'
	RENAME VALUE 'trivial' TO 'minor';
enum::values('priority');
Output
['minor', 'low', 'medium', 'normal', 'high', 'urgent', 'blocker']

RENAME VALUE changes the spelling of a value and keeps its position. It also rewrites the old value to the new one in every record and index entry of every field whose type names it, including inside an option, an array or a set, so the records keep passing the type check on their next write.

-- Setup from the example above
DEFINE TYPE priority AS 'low' | 'medium' | 'high';

DEFINE TABLE ticket SCHEMAFULL;
DEFINE FIELD priority ON ticket TYPE priority;
DEFINE FIELD history ON ticket TYPE array<priority> DEFAULT [];
DEFINE INDEX ticket_priority ON ticket FIELDS priority;

CREATE ticket:1 SET priority = 'medium', history = ['low', 'medium'];
CREATE ticket:2 SET priority = 'high';

ALTER TYPE priority RENAME VALUE 'medium' TO 'moderate';
SELECT * FROM ticket ORDER BY id;
Output
[
	{ history: ['low', 'moderate'], id: ticket:1, priority: 'moderate' },
	{ history: [], id: ticket:2, priority: 'high' }
]

The rewrite is maintenance by the database, not a write by a user. It writes only the records that hold the old value, rewrites a READONLY field, does not run the VALUE or DEFAULT clauses of the fields, and does not trigger any event. A table with a changefeed records each rewritten record as an update.

Renaming a value to one the type already lists is refused. A rename is also refused while a field holds the type in an array inside another array, such as array<array<priority>>, because the rewrite does not reach that depth:

DEFINE FIELD grid ON ticket TYPE option<array<array<priority>>>;
ALTER TYPE priority RENAME VALUE 'low' TO 'lowest';
Error output
"the type 'priority' is held by the field 'grid.*.*' on the table 'ticket', whose path a rename does not reach"

The same values can be written out on each field as a literal, or defined once with DEFINE TYPE and named by each field. While the tables are empty, the two forms behave the same. Once they hold records, a change to the values works differently. ALTER FIELD changes one field at a time, and compares its old definition with the new one. ALTER TYPE changes every field that names the type, and rewrites the records it needs to.

Here are the two forms, each with a priority field on two tables and a few records. Each one starts in a new database, because the examples above already define a priority type and a ticket table.

A literal on each field
DEFINE TABLE ticket SCHEMAFULL;
DEFINE FIELD priority ON ticket TYPE 'low' | 'medium' | 'high';
DEFINE TABLE task SCHEMAFULL;
DEFINE FIELD priority ON task TYPE 'low' | 'medium' | 'high';

CREATE ticket:1 SET priority = 'high';
CREATE ticket:2 SET priority = 'medium';
CREATE task:1 SET priority = 'medium';
A named type
DEFINE TYPE priority AS 'low' | 'medium' | 'high';
DEFINE TABLE ticket SCHEMAFULL;
DEFINE FIELD priority ON ticket TYPE priority;
DEFINE TABLE task SCHEMAFULL;
DEFINE FIELD priority ON task TYPE priority;

CREATE ticket:1 SET priority = 'high';
CREATE ticket:2 SET priority = 'medium';
CREATE task:1 SET priority = 'medium';

Adding a value works with both forms. The new literal accepts every value the old one did, so ALTER FIELD needs no migration, but it has to be run once for each field:

-- Setup from the examples above
DEFINE TABLE ticket SCHEMAFULL;
DEFINE FIELD priority ON ticket TYPE 'low' | 'medium' | 'high';
DEFINE TABLE task SCHEMAFULL;
DEFINE FIELD priority ON task TYPE 'low' | 'medium' | 'high';

CREATE ticket:1 SET priority = 'high';
CREATE ticket:2 SET priority = 'medium';
CREATE task:1 SET priority = 'medium';
DEFINE TYPE priority AS 'low' | 'medium' | 'high';

-- A literal on each field
ALTER FIELD priority ON ticket TYPE 'low' | 'medium' | 'high' | 'urgent';
ALTER FIELD priority ON task TYPE 'low' | 'medium' | 'high' | 'urgent';

-- A named type
ALTER TYPE priority ADD VALUE 'urgent';

Renaming a value is where the two forms differ. To ALTER FIELD, a rename is a new literal that no longer accepts 'medium', so a table with records refuses it until the statement says what those records become:

-- Setup from the example above
DEFINE TABLE ticket SCHEMAFULL;
DEFINE FIELD priority ON ticket TYPE 'low' | 'medium' | 'high';
DEFINE TABLE task SCHEMAFULL;
DEFINE FIELD priority ON task TYPE 'low' | 'medium' | 'high';

CREATE ticket:1 SET priority = 'high';
CREATE ticket:2 SET priority = 'medium';
CREATE task:1 SET priority = 'medium';

-- A literal on each field
ALTER FIELD priority ON ticket TYPE 'low' | 'normal' | 'high';
//- 'Cannot change field `priority` of table `ticket`: the table is SCHEMAFULL in a database at compatibility level 4 and has rows, and the new definition does not accept every value the old one did: add MIGRATE EXISTING ROWS'

ALTER FIELD priority ON ticket TYPE 'low' | 'normal' | 'high'
    MIGRATE EXISTING ROWS AS medium_to_normal
    USING { medium: 'normal' }[$value] ?? $value FALLBACK 'normal';

SELECT * FROM ticket ORDER BY id;
//- [{ id: ticket:1, priority: 'high' }, { id: ticket:2, priority: 'normal' }]
SELECT * FROM task;
//- [{ id: task:1, priority: 'medium' }]

The migration changed ticket only, and task still holds 'medium' until its own field is changed in the same way. With a named type, one RENAME VALUE rewrites the records of every field that names the type:

-- Setup from the example above
DEFINE TYPE priority AS 'low' | 'medium' | 'high';
DEFINE TABLE ticket SCHEMAFULL;
DEFINE FIELD priority ON ticket TYPE priority;
DEFINE TABLE task SCHEMAFULL;
DEFINE FIELD priority ON task TYPE priority;

CREATE ticket:1 SET priority = 'high';
CREATE ticket:2 SET priority = 'medium';
CREATE task:1 SET priority = 'medium';

-- A named type
ALTER TYPE priority RENAME VALUE 'medium' TO 'normal';

SELECT * FROM ticket ORDER BY id;
//- [{ id: ticket:1, priority: 'high' }, { id: ticket:2, priority: 'normal' }]
SELECT * FROM task;
//- [{ id: task:1, priority: 'normal' }]

Removing a value works the other way around. ALTER FIELD with MIGRATE EXISTING ROWS can narrow a literal and convert the records that held the removed value. ALTER TYPE has no clause to remove a value, and DEFINE TYPE ... OVERWRITE refuses a different list of values while a field names the type:

-- A named type
DEFINE TYPE OVERWRITE priority AS 'medium' | 'high';
//- "Invalid statement: Cannot change the values of the type 'priority': the field 'priority' on the table 'task' has a type that names it"

To remove a value from a named type, define a new type without it, and change each field to the new type with a migration.

ChangeA literal on each fieldA named type
Add a valueALTER FIELD on each field, without a migrationOne ALTER TYPE ... ADD VALUE
Rename a valueALTER FIELD on each field, with MIGRATE EXISTING ROWSOne ALTER TYPE ... RENAME VALUE, which rewrites the records
Remove a valueALTER FIELD on each field, with MIGRATE EXISTING ROWSA new type, then ALTER FIELD on each field with MIGRATE EXISTING ROWS

Was this page helpful?