Using link properties
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.
Declaration
Let’s a create a Person.friends link with a strength property corresponding to the strength of the friendship.
1. type Person {
2. required property name -> str { constraint exclusive };
4. multi link friends -> Person {
5. property strength -> float64;
6. }
7. }
Constraints
1. type Person {
2. required property name -> str { constraint exclusive };
4. multi link friends -> Person {
5. property strength -> float64;
6. constraint expression on (
7. __subject__@strength >= 0
8. );
9. }
10. }
Indexes
To index on a link property, you must declare an abstract link and extend it.
1. abstract link friendship {
2. property strength -> float64;
3. index on (__subject__@strength);
4. }
6. type Person {
7. required property name -> str { constraint exclusive };
8. multi link friends extending friendship -> Person;
9. }
Inserting
The @strength property is specified in the shape of the select subquery. This is only valid in a subquery inside an insert statement.
1. insert Person {
2. name := "Bob",
3. friends := (
4. select detached Person {
5. @strength := 3.14
6. }
7. filter .name = "Alice"
8. )
9. }
We are using the detached operator to unbind the Person reference from the scope of the insert query.
When doing a nested insert, link properties can be directly included in the inner insert subquery.
1. insert Person {
2. name := "Bob",
3. friends := (
4. insert Person {
5. name := "Jane",
6. @strength := 3.14
7. }
8. )
9. }
Updating
1. update Person
2. filter .name = "Bob"
3. set {
4. friends += (
5. select .friends {
6. @strength := 3.7
7. }
8. filter .name = "Alice"
9. )
10. };
The example updates the @strength property of Bob’s friends link to Alice to 3.7.
In the context of multi links the += operator works like an an insert/update operator.
To update one or more links in a multi link, you can select from the current linked objects, as the example does. Use a detached selection if you want to insert/update a wider selection of linked objects instead.
Querying
1. edgedb>
2. .......
3. .......
4. .......
5. .......
6. .......
1. select Person {
2. friends: {
3. name,
4. @strength
5. }
6. };
1. {
2. default::Person {name: 'Alice', friends: {}},
3. default::Person {
4. name: 'Bob',
5. friends: {
6. default::Person {name: 'Alice', @strength: 3.7}
7. }
8. },
9. }
︙
| See also |
| Data Model > Links > Link properties |
| SDL > Properties |
| DDL > Properties |
| Introspection > Object Types |
