-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anton Kovalevsky
committed
May 3, 2021
1 parent
615afcd
commit c101475
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,29 @@ | ||
# Identifiers | ||
|
||
Please visit PostgreSQL documentation to familiarize with `RETURNING` syntax: [Returning Data From Modified Rows](https://www.postgresql.org/docs/9.5/dml-returning.html) | ||
|
||
## Returning identifiers | ||
|
||
``` | ||
val savePet: Query[Pet, Int] = | ||
sql""" | ||
INSERT INTO pets ( | ||
VALUES ${petCodec.values} | ||
RETURNING id; | ||
""".query(int4) | ||
``` | ||
|
||
## Returning entities | ||
|
||
It's reasonable to return just an identifier in case there are no other database-managed columns. | ||
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. | ||
That means you need to query whole entity with RETURNING expression. | ||
``` | ||
val savePet: Query[NewPet, PersistedPet] = | ||
sql""" | ||
INSERT INTO #$PETS_TABLE_NAME (name, colour) | ||
VALUES ${newPetCodec.values} | ||
RETURNING id, name, colour, created_at, created_by; | ||
""".query(persistedPetCodec) | ||
``` | ||
Please note that you might want to write down all column names (not just `*`) to decouple entity and database representation. |