Links can contain properties. Due to how they’re persisted under the hood, link properties have a few additional constraints: they’re always single and optional.

Let’s a create a Person.friends link with a strength property corresponding to the strength of the friendship.

Copy
type Person {
  required property name -> str { constraint exclusive };

  multi link friends -> Person {
    property strength -> float64;
  }
}
Copy
type Person {
  required property name -> str { constraint exclusive };

  multi link friends -> Person {
    property strength -> float64;
    constraint expression on (
      __subject__@strength >= 0
    );
  }
}

To index on a link property, you must declare an abstract link and extend it.

Copy
abstract link friendship {
  property strength -> float64;
  index on (__subject__@strength);
}

type Person {
  required property name -> str { constraint exclusive };
  multi link friends extending friendship -> Person;
}
Copy
insert Person {
  name := "Bob",
  friends := (
    select detached Person {
      @strength := 3.14
    }
    filter .name = "Alice"
  )
}

The @strength property is specified in the shape of the select subquery. This is only valid in a subquery inside an insert statement.

We are using the detached operator to unbind the Person reference from the scope of the insert query.

Copy
update Person
filter .name = "Bob"
set {
  friends += (
    select detached Person {
      @strength := 3.7
    }
    filter .name = "Alice"
  )
};
Copy
edgedb> 
....... 
....... 
....... 
....... 
....... 
select Person {
  friends: {
    name,
    @strength
  }
};
{
  default::Person {name: 'Alice', friends: {}},
  default::Person {
    name: 'Bob',
    friends: {
      default::Person {name: 'Alice', @strength: 3.7}
    }
  },
}
Light
Dark
System