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
1. insert Person {
2. name := "Bob",
3. friends := (
4. select detached Person {
5. @strength := 3.14
6. }
7. filter .name = "Alice"
8. )
9. }
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.
Updating
1. update Person
2. filter .name = "Bob"
3. set {
4. friends += (
5. select detached Person {
6. @strength := 3.7
7. }
8. filter .name = "Alice"
9. )
10. };
Querying
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 |
