Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds gotcha with db defaults to README #99

Merged
merged 2 commits into from
Oct 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,37 @@ r = MyRecord.new(id: 1, name: 'bar')
r.upsert!
```

### Gotcha with database defaults

When a table is defined with a database default for a field, this gotcha can occur when trying to explicitly upsert a record _to_ the default value (from a non-default value).

**Example**: a table called `hardwares` has a `prio` column with a default value.

┌─────────┬─────────┬───────┬
│ Column │ Type │Default│
├─────────┼─────────┼───────┼
│ id │ integer │ ...
│ prio │ integer │ 999

And `hardwares` has a record with a non-default value for `prio`. Say, the record with `id` 1 has a `prio` of `998`.

In this situation, upserting like:

```ruby
hw = { id: 1, prio: 999 }
Hardware.new(prio: hw[:prio]).upsert
```

will not mention the `prio` column in the `ON CONFLICT` clause, resulting in no update.

However, upserting like so:

```ruby
Hardware.upsert(prio: hw[:prio]).id
```

will indeed update the record in the database back to its default value, `999`.

### Conflict Clauses

It's possible to specify which columns should be used for the conflict clause. **These must comprise a unique index in Postgres.**
Expand Down