Redefine query

Redefine queries are used to modify existing types, capabilities, annotations, or functions in the schema, or to rename types.

Syntax

Redefine queries start with the redefine keyword following by a single definition statement of the define queries format, or a single type renaming statement.

redefine
  <definition_statement>
Type renaming statement
[<kind>] <label> label <new label>;
Role type renaming statement
<relation label>:<role label> label <new role label>;

Behavior

Redefine queries can be used to modify types and type capabilities, to rename types, or to override functions.

For clarity and precision, only one redefinition is allowed per query.

Renaming a type changes only its label: its data, capabilities, annotations, and position in the type hierarchy are preserved. Functions are not updated to the new label, so if a stored function refers to a renamed type by its old label, the commit fails.

An error is returned (on execution or on commit), and no changes are preserved, if:

  • There is no existing definition to redefine.

  • The redefinition matches the definition and does nothing.

  • There are multiple changes in the schema in the result of the redefinition.

  • The query results in an invalid schema.

  • The new label of a renamed type is already in use (for role types, within the same relation type hierarchy).

Statements

Examples

Schema for the following examples
#!test[schema, commit]
define
  entity content owns id;
  entity page sub content,
    owns page-id,
    owns name;
  entity profile sub page,
    owns username;
  entity user sub profile,
    owns phone,
    owns karma;

  attribute id value string;
  attribute page-id sub id;
  attribute username sub page-id;
  attribute name value string;
  attribute phone value string;
  attribute karma value double;
attribute bio value string;
attribute profile-picture value string;
attribute email @independent, value string @regex("^.*@.*\.com");
user owns bio, owns profile-picture, owns email;
attribute tag value string;
entity post owns tag @card(0..);
relation parentship relates parent, relates child;
relation fathership sub parentship, relates father as child;

Redefining types' capabilities and annotations

Redefining type’s sub
#!test[schema]
redefine user sub page;
Redefining attribute type’s value type
#!test[schema]
redefine karma value integer;
Redefining value type’s annotation with arguments
#!test[schema]
redefine email value string @regex("^.*@typedb\.com$");
Redefining type capability’s annotation with arguments
#!test[schema]
redefine post owns tag @card(0..5);
Redefining relates specialization
#!test[schema]
redefine fathership relates father as parent;

Renaming types

Renaming an entity type
#!test[schema]
redefine entity post label article;
Renaming an attribute type without specifying its kind
#!test[schema]
redefine tag label hashtag;
Renaming a role type
#!test[schema]
redefine parentship:child label offspring;

Redefining functions

Everything except for the function name can be redefined using redefine queries.

#!test[schema]
redefine
  fun karma_with_squared_value($karma: karma) -> double:
    match
      let $karma-squared = $karma * $karma;
    return first $karma-squared;