Search
ctrl/
Ask AI
Light
Dark
System

Insert

Insert new data with e.insert.

Copy
e.insert(e.Movie, {
  title: e.str("Spider-Man: No Way Home"),
  release_year: e.int64(2021)
});

For convenience, the second argument of e.insert function can also accept plain JS data or a named tuple.

Copy
e.insert(e.Movie, {
  title: "Spider-Man: No Way Home",
  actors: e.select(e.Person, person => ({
    filter: e.op(person.name, "=", "Robert Downey Jr."),
    '@character_name': e.str("Iron Man")
  }))
});
Copy
e.params({
  movie: e.tuple({
    title: e.str,
    release_year: e.int64
  })
}, $ =>
  e.insert(e.Movie, $.movie)
);

In EdgeQL, “upsert” functionality is achieved by handling conflicts on insert statements with the unless conflict clause. In the query builder, this is possible with the .unlessConflict method (available only on insert expressions).

In the simplest case, adding .unlessConflict (no arguments) will prevent EdgeDB from throwing an error if the insertion would violate an exclusivity contstraint. Instead, the query returns an empty set (null).

Copy
e.insert(e.Movie, {
  title: "Spider-Man: No Way Home",
  release_year: 2021
}).unlessConflict();
// => null

Provide an on clause to “catch” conflicts only on a specific property/link.

Copy
e.insert(e.Movie, {
  title: "Spider-Man: No Way Home",
  release_year: 2021
}).unlessConflict(movie => ({
  on: movie.title, // can be any expression
}));

You can also provide an else expression which will be executed and returned in case of a conflict. You must specify an on clause in order to use else.

The following query simply returns the pre-existing (conflicting) object.

Copy
e.insert(e.Movie, {
  title: "Spider-Man: Homecoming",
  release_year: 2021
}).unlessConflict(movie => ({
  on: movie.title,
  else: movie
}));

Or you can perform an upsert operation with an e.update in the else.

Copy
e.insert(e.Movie, {
  title: "Spider-Man: Homecoming",
  release_year: 2021
}).unlessConflict(movie => ({
  on: movie.title,
  else: e.update(movie, () => ({
    set: {
      release_year: 2021
    }
  })),
}));

You can use a for loop to perform bulk inserts.