Allows you to return data from modified rows.
On supported databases like PostgreSQL, this method can be chained to insert, update, delete and merge queries to return data.
insert
update
delete
merge
Note that on SQLite you need to give aliases for the expressions to avoid this bug in SQLite. For example .returning('id as id').
.returning('id as id')
Also see the returningAll method.
Return one column:
const { id } = await db .insertInto('person') .values({ first_name: 'Jennifer', last_name: 'Aniston' }) .returning('id') .executeTakeFirstOrThrow() Copy
const { id } = await db .insertInto('person') .values({ first_name: 'Jennifer', last_name: 'Aniston' }) .returning('id') .executeTakeFirstOrThrow()
Return multiple columns:
const { id, last_name } = await db .insertInto('person') .values({ first_name: 'Jennifer', last_name: 'Aniston' }) .returning(['id', 'last_name']) .executeTakeFirstOrThrow() Copy
const { id, last_name } = await db .insertInto('person') .values({ first_name: 'Jennifer', last_name: 'Aniston' }) .returning(['id', 'last_name']) .executeTakeFirstOrThrow()
Return arbitrary expressions:
import { sql } from 'kysely'const { id, full_name, first_pet_id } = await db .insertInto('person') .values({ first_name: 'Jennifer', last_name: 'Aniston' }) .returning((eb) => [ 'id as id', sql<string>`concat(first_name, ' ', last_name)`.as('full_name'), eb.selectFrom('pet').select('pet.id').limit(1).as('first_pet_id') ]) .executeTakeFirstOrThrow() Copy
import { sql } from 'kysely'const { id, full_name, first_pet_id } = await db .insertInto('person') .values({ first_name: 'Jennifer', last_name: 'Aniston' }) .returning((eb) => [ 'id as id', sql<string>`concat(first_name, ' ', last_name)`.as('full_name'), eb.selectFrom('pet').select('pet.id').limit(1).as('first_pet_id') ]) .executeTakeFirstOrThrow()
Adds a returning * to an insert/update/delete/merge query on databases that support returning such as PostgreSQL.
returning *
returning
Also see the returning method.
Allows you to return data from modified rows.
On supported databases like PostgreSQL, this method can be chained to
insert
,update
,delete
andmerge
queries to return data.Note that on SQLite you need to give aliases for the expressions to avoid this bug in SQLite. For example
.returning('id as id')
.Also see the returningAll method.
Examples
Return one column:
Return multiple columns:
Return arbitrary expressions: