Skip to content

Commit c101475

Browse files
author
Anton Kovalevsky
committed
RETURNING docs
1 parent 615afcd commit c101475

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# Identifiers
22

3+
Please visit PostgreSQL documentation to familiarize with `RETURNING` syntax: [Returning Data From Modified Rows](https://www.postgresql.org/docs/9.5/dml-returning.html)
4+
5+
## Returning identifiers
6+
7+
```
8+
val savePet: Query[Pet, Int] =
9+
sql"""
10+
INSERT INTO pets (
11+
VALUES ${petCodec.values}
12+
RETURNING id;
13+
""".query(int4)
14+
```
15+
16+
## Returning entities
17+
18+
It's reasonable to return just an identifier in case there are no other database-managed columns.
19+
Let's assume we have five columns: `id`, `name`, `colour`, `created_at`, `created_by` where `id`, `created_at`, `created_by` are not part of initial `NewPet` entity.
20+
That means you need to query whole entity with RETURNING expression.
21+
```
22+
val savePet: Query[NewPet, PersistedPet] =
23+
sql"""
24+
INSERT INTO #$PETS_TABLE_NAME (name, colour)
25+
VALUES ${newPetCodec.values}
26+
RETURNING id, name, colour, created_at, created_by;
27+
""".query(persistedPetCodec)
28+
```
29+
Please note that you might want to write down all column names (not just `*`) to decouple entity and database representation.

0 commit comments

Comments
 (0)