Update

The update command is used to update existing objects.

  1. db>
  2. ...
  3. ...
  1. update Hero
  2. filter .name = "Hawkeye"
  3. set { name := "Ronin" };
  1. {default::Hero {id: d476b12e-3e7b-11ec-af13-2717f3dc1d8a}}

If you omit the filter clause, all objects will be updated. This is useful for updating values across all objects of a given type. The example below cleans up all Hero.name values by trimming whitespace and converting them to title case.

  1. db>
  2. ...
  1. update Hero
  2. set { name := str_trim(str_title(.name)) };
  1. {default::Hero {id: d476b12e-3e7b-11ec-af13-2717f3dc1d8a}}

Syntax

The structure of the update statement (update...filter...set) is an intentional inversion of SQL’s UPDATE...SET...WHERE syntax. Curiously, in SQL, the where clauses typically occurs last despite being applied before the set statement. EdgeQL is structured to reflect this; first, a target set is specified, then filters are applied, then the data is updated.

When updating links, the := operator will replace the set of linked values.

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...
  1. update movie
  2. filter .title = "Black Widow"
  3. set {
  4. characters := (
  5. select Person
  6. filter .name in { "Black Widow", "Yelena", "Dreykov" }
  7. )
  8. };
  1. {default::Title {id: af706c7c-3e98-11ec-abb3-4bbf3f18a61a}}
  1. db>
  2. ...
  1. select Movie { num_characters := count(.characters) }
  2. filter .title = "Black Widow";
  1. {default::Movie {num_characters: 3}}

To add additional linked items, use the += operator.

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  1. update Movie
  2. filter .title = "Black Widow"
  3. set {
  4. characters += (insert Villain {name := "Taskmaster"})
  5. };
  1. {default::Title {id: af706c7c-3e98-11ec-abb3-4bbf3f18a61a}}
  1. db>
  2. ...
  1. select Movie { num_characters := count(.characters) }
  2. filter .title = "Black Widow";
  1. {default::Movie {num_characters: 4}}

To remove items, use -=.

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  1. update Movie
  2. filter .title = "Black Widow"
  3. set {
  4. characters -= Villain # remove all villains
  5. };
  1. {default::Title {id: af706c7c-3e98-11ec-abb3-4bbf3f18a61a}}
  1. db>
  2. ...
  1. select Movie { num_characters := count(.characters) }
  2. filter .title = "Black Widow";
  1. {default::Movie {num_characters: 2}}

With blocks

All top-level EdgeQL statements (select, insert, update, and delete) can be prefixed with a with block. This is useful for updating the results of a complex query.

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...
  1. with people := (
  2. select Person
  3. order by .name
  4. offset 3
  5. limit 3
  6. )
  7. update people
  8. set { name := str_trim(.name) };
  1. {
  2. default::Hero {id: d4764c66-3e7b-11ec-af13-df1ba5b91187},
  3. default::Hero {id: d7d7e0f6-40ae-11ec-87b1-3f06bed494b9},
  4. default::Villain {id: d477a836-3e7b-11ec-af13-4fea611d1c31},
  5. }

You can pass any object-type expression into update, including polymorphic ones (as above).

See also

For documentation on performing upsert operations, see EdgeQL > Insert > Upserts.

Reference > Commands > Update

Cheatsheets > Updating data