Skip to content

Commit

Permalink
RETURNING docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Kovalevsky committed May 3, 2021
1 parent 615afcd commit 9a387e2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions modules/docs/src/main/paradox/reference/Identifiers.md
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 enties

It's reasonable to return just an identifier in case there are no other database-managed columns.
Let 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.

0 comments on commit 9a387e2

Please sign in to comment.