edgedb / edgedb-python

The official Python client library for EdgeDB
https://edgedb.com
Apache License 2.0
369 stars 44 forks source link

How does one return an object id on insert #305

Closed shomodj closed 2 years ago

shomodj commented 2 years ago

I'm looking at the INSERT snippet from the https://www.edgedb.com/docs/edgeql/insert#basic-usage

insert Hero {
  name := "Spider-Man",
  secret_identity := "Peter Parker"
};

and it looks to me that EdgeDB returns an object ID on the CLI, but when I run it from Python I get

Set{Object{}}

Basically, I want to do what Postgres does at https://www.postgresql.org/docs/current/dml-returning.html

INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id;

Is this possible?

Thanks.

elprans commented 2 years ago

Actually, ids are always fetched by default, they're just not included in the repr:

>>> conn.query('insert Hero')[0].id
UUID('5b214b7a-c7fd-11ec-8145-dfc85a3fcb32')

If you want to select other fields, wrap insert into a select:

select (
  insert Hero {
    name := "Spider-Man",
    secret_identity := "Peter Parker"
  }
) {
  id,
  name,
  secret_identity,
};

A more readable formulation would be using a with clause:

with
  NewHero := (
    insert Hero {
      name := "Spider-Man",
      secret_identity := "Peter Parker"
    }
  )
select
  NewHero {
    id,
    name,
    secret_identity,
  }
shomodj commented 2 years ago

@shomodj thanks, It's the repr fault ;) I'll update the docs, this info is valuable.