Skip to content

Commit

Permalink
Update documentation on upsert_all so that it is correct for Postgres
Browse files Browse the repository at this point in the history
Details in rails#35519

In short, MySQL and Sqlite3 allow a record to be both inserted _and_ replaced in the same operation. Postgres (and the SQL-2003 rules for MERGE) do not.

Postgres's rationale seems to be that the operation would be nondeterministic.

I think it's OK for Rails users to have a different experience with this feature depending on their database; but I think you should be able to follow the examples in the docs on any database.
  • Loading branch information
boblail committed Mar 8, 2019
1 parent 199de6b commit 02441e8
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions activerecord/lib/active_record/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,15 @@ def upsert(attributes, returning: nil, unique_by: nil)
#
# ==== Examples
#
# # Insert multiple records, performing an upsert when records have duplicate ISBNs
# # Given a Unique Index on books.isbn and the following record:
# Book.create!(title: 'Rework', author: 'David', isbn: '1')
#
# # Insert multiple records, allowing new records with the same ISBN
# # as an existing record to overwrite the existing record.
# # ('Eloquent Ruby' will overwrite 'Rework' because its ISBN is duplicate)
# Book.upsert_all([
# { title: 'Rework', author: 'David', isbn: '1' },
# { title: 'Eloquent Ruby', author: 'Russ', isbn: '1' }
# { title: 'Eloquent Ruby', author: 'Russ', isbn: '1' },
# { title: 'Clean Code', author: 'Robert', isbn: '2' }
# ],
# unique_by: { columns: %w[ isbn ] })
#
Expand Down

0 comments on commit 02441e8

Please sign in to comment.