1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-07 00:58:02 +00:00

Add Returning With Sequel as a ruby til

This commit is contained in:
jbranchaud
2016-01-25 12:33:15 -06:00
parent 05845a1796
commit 1f1796c9ea
2 changed files with 22 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
# Returning With Sequel
The [`sequel`](https://github.com/jeremyevans/sequel) gem is a database
toolkit that allows you to interact with most databases. PostgreSQL has
support for composite primary keys, but `sequel`, which is supposed to return
the `id` of newly inserted records, isn't sure what to return when faced
with a composite primary key. You can get around this by telling `sequel`
exactly what should be returned using the `#returning` method. For instance,
get it to return just the `id` of the new record:
```ruby
DB[:floors].returning(:id).insert(hotel_id: 4, id: 1, ...)
# [{id: 1}]
```
To get it to return both parts of composite key:
```ruby
DB[:floors].returning(:id, :hotel_id).insert(hotel_id: 4, id: 1, ...)
# [{id: 1, hotel_id: 4}]
```