Links

This section describes the DDL commands pertaining to links.

Define a new link.

  1. [ with with-item [, ...] ]
  2. {create|alter} type TypeName "{"
  3. [ ... ]
  4. create [{required | optional}] [{single | multi}]
  5. link name
  6. [ extending base [, ...] ] -> type
  7. [ "{" subcommand; [...] "}" ] ;
  8. [ ... ]
  9. "}"
  10. Computed link form:
  11. [ with with-item [, ...] ]
  12. {create|alter} type TypeName "{"
  13. [ ... ]
  14. create [{required | optional}] [{single | multi}]
  15. link name := expression;
  16. [ ... ]
  17. "}"
  18. Abstract link form:
  19. [ with with-item [, ...] ]
  20. create abstract link [module::]name [extending base [, ...]]
  21. [ "{" subcommand; [...] "}" ]
  22. where subcommand is one of
  23. set default := expression
  24. set readonly := {true | false}
  25. create annotation annotation-name := value
  26. create property property-name ...
  27. create constraint constraint-name ...
  28. on target delete action
  29. reset on target delete
  30. create index on index-expr

Description

The combinations of create type ... create link and alter type ... create link define a new concrete link for a given object type.

There are three forms of create link, as shown in the syntax synopsis above. The first form is the canonical definition form, the second form is a syntax shorthand for defining a computed link, and the third is a form to define an abstract link item. The abstract form allows creating the link in the specified module. Concrete link forms are always created in the same module as the containing object type.

Parameters

Most sub-commands and options of this command are identical to the SDL link declaration. The following subcommands are allowed in the create link block:

set default := expression

Specifies the default value for the link as an EdgeQL expression. Other than a slight syntactical difference this is the same as the corresponding SDL declaration.

set readonly := {true | false}

Specifies whether the link is considered read-only. Other than a slight syntactical difference this is the same as the corresponding SDL declaration.

create annotation annotation-name := value;

Add an annotation annotation-name set to value to the type.

See create annotation for details.

create property property-name …

Define a concrete property item for this link. See create property for details.

create constraint constraint-name …

Define a concrete constraint for this link. See create constraint for details.

on target delete action

Valid values for action are: restrict, DELETE SOURCE, allow, and deferred restrict. The details of what on target delete options mean are described in this section.

reset on target delete

Reset the delete policy to either the inherited value or to the default restrict. The details of what on target delete options mean are described in this section.

create index on index-expr

Define a new index using index-expr for this link. See create index for details.

Examples

Define a new link friends on the User object type:

  1. alter type User {
  2. create multi link friends -> User
  3. };

Define a new computed link special_group on the User object type, which contains all the friends from the same town:

  1. alter type User {
  2. create link special_group := (
  3. select __source__.friends
  4. filter .town = __source__.town
  5. )
  6. };

Define a new abstract link orderable and a concrete link interests that extends it, inheriting its weight property:

  1. create abstract link orderable {
  2. create property weight -> std::int64
  3. };
  4. alter type User {
  5. create multi link interests extending orderable -> Interest
  6. };

Change the definition of a link.

  1. [ with with-item [, ...] ]
  2. {create|alter} type TypeName "{"
  3. [ ... ]
  4. alter link name
  5. [ "{" ] subcommand; [...] [ "}" ];
  6. [ ... ]
  7. "}"
  8. [ with with-item [, ...] ]
  9. alter abstract link [module::]name
  10. [ "{" ] subcommand; [...] [ "}" ];
  11. where subcommand is one of
  12. set default := expression
  13. reset default
  14. set readonly := {true | false}
  15. reset readonly
  16. rename to newname
  17. extending ...
  18. set required
  19. set optional
  20. reset optionality
  21. set single
  22. set multi
  23. reset cardinality
  24. set type typename [using (<conversion-expr)]
  25. reset type
  26. using (computed-expr)
  27. create annotation annotation-name := value
  28. alter annotation annotation-name := value
  29. drop annotation annotation-name
  30. create property property-name ...
  31. alter property property-name ...
  32. drop property property-name ...
  33. create constraint constraint-name ...
  34. alter constraint constraint-name ...
  35. drop constraint constraint-name ...
  36. on target delete action
  37. create index on index-expr
  38. drop index on index-expr

Description

The combinations of``create type … alter link`` and alter type ... alter link change the definition of a concrete link for a given object type.

The command alter abstract link changes the definition of an abstract link item. name must be the identity of an existing abstract link, optionally qualified with a module name.

Parameters

The following subcommands are allowed in the alter link block:

rename to newname

Change the name of the link item to newname. All concrete links inheriting from this links are also renamed.

extending …

Alter the link parent list. The full syntax of this subcommand is:

  1. extending name [, ...]
  2. [ first | last | before parent | after parent ]

This subcommand makes the link a child of the specified list of parent links. The requirements for the parent-child relationship are the same as when creating a link.

It is possible to specify the position in the parent list using the following optional keywords:

  • first – insert parent(s) at the beginning of the parent list,

  • last – insert parent(s) at the end of the parent list,

  • before <parent> – insert parent(s) before an existing parent,

  • after <parent> – insert parent(s) after an existing parent.

set required

Make the link required.

set optional

Make the link no longer required (i.e. make it optional).

reset optionality

Reset the optionality of the link to the default value (optional), or, if the link is inherited, to the value inherited from links in supertypes.

set single

Change the link set’s maximum cardinality to one. Only valid for concrete links.

set multi

Remove the upper limit on the link set’s cardinality. Only valid for concrete links.

reset cardinality

Reset the link set’s maximum cardinality to the default value (single), or to the value inherited from the link’s supertypes.

set type typename [using (<conversion-expr)]

Change the type of the link to the specified typename. The optional using clause specifies a conversion expression that computes the new link value from the old. The conversion expression must return a singleton set and is evaluated on each element of multi links. A using clause must be provided if there is no implicit or assignment cast from old to new type.

reset type

Reset the type of the link to be strictly the inherited type. This only has an effect on links that have been overloaded in order to change their inherited type. It is an error to reset type on a link that is not inherited.

using (computed-expr)

Change the expression of a computed link. Only valid for concrete links.

alter annotation annotation-name;

Alter link annotation annotation-name. See alter annotation for details.

drop annotation annotation-name;

Remove link item’s annotation annotation-name. See drop annotation for details.

alter property property-name …

Alter the definition of a property item for this link. See alter property for details.

drop property property-name;

Remove a property item from this link. See drop property for details.

alter constraint constraint-name …

Alter the definition of a constraint for this link. See alter constraint for details.

drop constraint constraint-name;

Remove a constraint from this link. See drop constraint for details.

drop index on index-expr

Remove an index defined on index-expr from this link. See drop index for details.

reset default

Remove the default value from this link, or reset it to the value inherited from a supertype, if the link is inherited.

reset readonly

Set link writability to the default value (writable), or, if the link is inherited, to the value inherited from links in supertypes.

All the subcommands allowed in the create link block are also valid subcommands for alter link block.

Examples

On the object type User, set the title annotation of its friends link to "Friends":

  1. alter type User {
  2. alter link friends create annotation title := "Friends";
  3. };

Rename the abstract link orderable to sorted:

  1. alter abstract link orderable rename to sorted;

Redefine the computed link special_group to be those who have some shared interests:

  1. alter type User {
  2. create link special_group := (
  3. select __source__.friends
  4. # at least one of the friend's interests
  5. # must match the user's
  6. filter .interests IN __source__.interests
  7. )
  8. };

Remove the specified link from the schema.

  1. [ with with-item [, ...] ]
  2. alter type TypeName "{"
  3. [ ... ]
  4. drop link name
  5. [ ... ]
  6. "}"
  7. [ with with-item [, ...] ]
  8. drop abstract link [module]::name

Description

The combination of alter type and drop link removes the specified link from its containing object type. All links that inherit from this link are also removed.

The command drop abstract link removes an existing link item from the database schema. All subordinate schema items defined on this link, such as link properties and constraints, are removed as well.

Examples

Remove link friends from object type User:

  1. alter type User drop link friends;

Drop abstract link orderable:

  1. drop abstract link orderable;

See also

Schema > Links

SDL > Links

Introspection > Object types