stablekernel / postgresql-dart

Dart PostgreSQL driver: supports extended query format, binary protocol and statement reuse.
https://www.dartdocs.org/documentation/postgres/latest
BSD 3-Clause "New" or "Revised" License
129 stars 32 forks source link

[question] get last inserted id missing? #148

Closed moovida closed 3 years ago

moovida commented 3 years ago

Hi there, great library!

I was wondering if there is the possibility to get the last inserted id from an insert query that has an auto-incrementing primary key. Thank you.

alanfzf commented 3 years ago

I need this feature too!

Gustl22 commented 3 years ago

You can retrieve the primary key like this:

final primaryKeyName = 'id';
final res = await yourConnection.query('INSERT INTO $tableName ($keys) VALUES ($values) RETURNING $primaryKeyName;');
final lastInsertedId = res.last[0] as int;