Redefine query

Redefine queries are used to modify existing types, type interfaces, or functions in the schema.

Syntax

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

redefine
  <definition_statement>

Behavior

Redefine queries can be used to modify types and type interfaces or to override functions.

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

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.

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;
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' interfaces 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 interface’s annotation with arguments
#!test[schema]
redefine post owns tag @card(0..5);
Redefining relates specialization
#!test[schema]
redefine fathership relates father as parent;

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;