Light
Dark
System

Links

Links define a specific relationship between two object types.

See the Modeling Relations guide for a breakdown of how to model one-to-one, one-to-many, and many-to-many relationships in EdgeDB.

You can add an exclusive constraint to a link to guarantee that no other instances can link to the same target(s).

Copy
type Person {
  property name -> str;
}

type GroupChat {
  required multi link members -> Person {
    constraint exclusive;
  }
}

In the GroupChat example, the GroupChat.members link is now exclusive. No two GroupChats can link to the same Person; put differently, no Person can be a member of multiple GroupChats.

The combination of link cardinality and exclusive constraints are sufficient to model all kinds of relations: one-to-one, one-to-many, and many-to-many. For details, read the Modeling Relations guide.

Like properties, links can declare a default value in the form of an EdgeQL expression, which will be executed upon insertion. In the example below, new people are automatically assigned three random friends.

Copy
type Person {
  required property name -> str;
  multi link friends -> Person {
    default := (select Person order by random() limit 3);
  }
}

Links can declare their own deletion policy. There are two kinds of events that might trigger these policies: target deletion and source deletion.

Target deletion policies determine what action should be taken when the target of a given link is deleted. They are declared with the on target delete clause.

Copy
type MessageThread {
  property title -> str;
}

type Message {
  property content -> str;
  link chat -> MessageThread {
    on target delete delete source;
  }
}

The Message.chat link in the example uses the delete source policy. There are 4 available target deletion policies.

  • restrict (default) - Any attempt to delete the target object immediately raises an exception.

  • delete source - when the target of a link is deleted, the source is also deleted. This is useful for implementing cascading deletes.

    There is a limit to the depth of a deletion cascade due to an upstream stack size limitation.

  • allow - the target object is deleted and is removed from the set of the link targets.

  • deferred restrict - any attempt to delete the target object raises an exception at the end of the transaction, unless by that time this object is no longer in the set of link targets.

Only available in EdgeDB 2.0 or later.

Source deletion policies determine what action should be taken when the source of a given link is deleted. They are declared with the on source delete clause.

Copy
type MessageThread {
  property title -> str;
  multi link messages -> Message {
    on source delete delete target;
  }
}

type Message {
  property content -> str;
}

Under this policy, deleting a MessageThread will unconditionally delete its messages as well.

To avoid deleting a Message that is linked to by other schema entities, append if orphan.

Copy
type MessageThread {
  property title -> str;
  multi link messages -> Message {
    on source delete delete target;
    on source delete delete target if orphan;
  }
}
Light
Dark
System