Update
update
– update objects in a database
[ with with-item [, ...] ]
update selector-expr
[ filter filter-expr ]
set shape ;
update
changes the values of the specified links in all objects
selected by update-selector-expr and, optionally, filtered by
filter-expr.
- with
-
Alias declarations.
The
with
clause allows specifying module aliases as well as expression aliases that can be referenced by theupdate
statement. See With block for more information. - update selector-expr
-
An arbitrary expression returning a set of objects to be updated.
- filter filter-expr
-
An expression of type
bool
used to filter the set of updated objects.filter-expr is an expression that has a result of type
bool
. Only objects that satisfy the filter expression will be updated. See the description of thefilter
clause of theselect
statement for more information. - set shape
-
A shape expression with the new values for the links of the updated object. There are three possible assignment operations permitted within the
set
shape:set { field := update-expr [, ...] } set { field += update-expr [, ...] } set { field -= update-expr [, ...] }
The most basic assignment is the
:=
, which just sets the field to the specified update-expr. The+=
and-=
either add or remove the set of values specified by the update-expr from the current value of the field.
Examples
Here are a couple of examples of the update
statement with simple
assignments using :=
:
# update the user with the name 'Alice Smith'
with module example
update User
filter .name = 'Alice Smith'
set {
name := 'Alice J. Smith'
};
# update all users whose name is 'Bob'
with module example
update User
filter .name like 'Bob%'
set {
name := User.name ++ '*'
};
For usage of +=
and -=
consider the following Post
type:
# ... Assume some User type is already defined
type Post {
required title: str;
required body: str;
# A "tags" property containing a set of strings
multi tags: str;
author: User;
}
The following queries add or remove tags from some user’s posts:
with module example
update Post
filter .author.name = 'Alice Smith'
set {
# add tags
tags += {'example', 'edgeql'}
};
with module example
update Post
filter .author.name = 'Alice Smith'
set {
# remove a tag, if it exist
tags -= 'todo'
};
The statement for <x> in <expr>
allows to express certain bulk
updates more clearly. See
Usage of for statement for more details.