Skip to content

Commit 9b449e3

Browse files
committed
test: use adapter-specific :on_duplicate_key_update shape
MySQL rejects the :conflict_target hash key (treats it as a column: "Unknown column 'conflict_target'"). It infers the conflicting key on its own, so it takes the bare column list, while PostgreSQL/SQLite need the explicit conflict_target. Branch the option in the spec and document both shapes in the README.
1 parent 54069d1 commit 9b449e3

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ update colliding rows and insert new ones in a single pass with
241241

242242
```ruby
243243
ActiveAdmin.register Author do
244+
# PostgreSQL / SQLite
244245
active_admin_import validate: false,
245246
on_duplicate_key_update: {
246247
conflict_target: [:id],
@@ -251,8 +252,16 @@ end
251252

252253
Notes:
253254

254-
* `conflict_target` names the unique column(s) used to detect a collision
255-
(`[:id]` for the primary key). MySQL infers it and ignores this option.
255+
* The option shape is **adapter-specific**, since it is passed straight to
256+
`activerecord-import`:
257+
* PostgreSQL / SQLite need an explicit `:conflict_target` — the unique
258+
column(s) used to detect a collision (`[:id]` for the primary key).
259+
* MySQL infers the conflicting key, so pass just the column list and omit
260+
`:conflict_target` (passing it raises `Unknown column 'conflict_target'`):
261+
262+
```ruby
263+
on_duplicate_key_update: %i[name last_name birthday]
264+
```
256265
* Turn `validate` off for id-based upserts. `activerecord-import` runs
257266
uniqueness validations against the very rows the upsert is about to overwrite,
258267
so a model-level `validates_uniqueness_of` would otherwise reject the update.

spec/import_spec.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,21 @@ def upload_file!(name, ext = 'csv')
228228
Author.delete_all
229229
Author.create!(id: 1, name: 'John', last_name: 'Doe', birthday: '1900-01-01')
230230

231+
# The option shape is adapter-specific: MySQL infers the conflicting key
232+
# and only wants the column list, while PostgreSQL/SQLite need an explicit
233+
# :conflict_target (see README).
234+
on_duplicate_key_update =
235+
if ActiveRecord::Base.connection.adapter_name.match?(/mysql/i)
236+
%i[name last_name birthday]
237+
else
238+
{ conflict_target: [:id], columns: %i[name last_name birthday] }
239+
end
240+
231241
add_author_resource(
232242
# Uniqueness validation runs against the rows the upsert is about to
233243
# overwrite, so it must be off for an id-based upsert (see README).
234244
validate: false,
235-
on_duplicate_key_update: {
236-
conflict_target: [:id],
237-
columns: %i[name last_name birthday]
238-
}
245+
on_duplicate_key_update: on_duplicate_key_update
239246
)
240247
visit '/admin/authors/import'
241248
upload_file!(:authors_with_ids)

0 commit comments

Comments
 (0)