Skip to content

Commit 1f1796c

Browse files
committed
Add Returning With Sequel as a ruby til
1 parent 05845a1 commit 1f1796c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ _305 TILs and counting..._
231231
- [Rake Only Lists Tasks With Descriptions](ruby/rake-only-lists-tasks-with-descriptions.md)
232232
- [Replace The Current Process With An External Command](ruby/replace-the-current-process-with-an-external-command.md)
233233
- [Rendering ERB](ruby/rendering-erb.md)
234+
- [Returning With Sequel](ruby/returning-with-sequel.md)
234235
- [Safe Navigation Operator](ruby/safe-navigation-operator.md)
235236
- [Set RVM Default Ruby](ruby/set-rvm-default-ruby.md)
236237
- [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md)

ruby/returning-with-sequel.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Returning With Sequel
2+
3+
The [`sequel`](https://github.com/jeremyevans/sequel) gem is a database
4+
toolkit that allows you to interact with most databases. PostgreSQL has
5+
support for composite primary keys, but `sequel`, which is supposed to return
6+
the `id` of newly inserted records, isn't sure what to return when faced
7+
with a composite primary key. You can get around this by telling `sequel`
8+
exactly what should be returned using the `#returning` method. For instance,
9+
get it to return just the `id` of the new record:
10+
11+
```ruby
12+
DB[:floors].returning(:id).insert(hotel_id: 4, id: 1, ...)
13+
# [{id: 1}]
14+
```
15+
16+
To get it to return both parts of composite key:
17+
18+
```ruby
19+
DB[:floors].returning(:id, :hotel_id).insert(hotel_id: 4, id: 1, ...)
20+
# [{id: 1, hotel_id: 4}]
21+
```

0 commit comments

Comments
 (0)