EdgeQL Tutorial

Upsert

Subtopics

Sometimes you need to either insert a new object or update an existing one identified with some key (possibly something other than the main id). This is typical of situations like this tutorial, where an object may or may not already exist and needs to be reset back to some specific state. This kind of operation is often called an "upsert" - a portmanteau of "update or insert". In EdgeQL this is achieved by appending UNLESS CONFLICT clause to a regular INSERT:

Input
Run
Output
Press the 'Run' button to evaluate the input

Let's make sure that we've ended up with the right user:

Input
Run
Output
Press the 'Run' button to evaluate the input

Now, just to prove that this "upsert" operation doesn't create a new user every time, let's run it again to update the name:

Input
Run
Output
Press the 'Run' button to evaluate the input

See that although the user's name is updated, the id is the same as before:

Input
Run
Output
Press the 'Run' button to evaluate the input