Closed dounan closed 4 years ago
We have a foo table that has two unique indices:
foo
t.index ["a", "b", "c"], unique: true, using: :btree t.index ["a", "b"], unique: true, where: "(c IS NULL)", using: :btree
In our model, we specify the upsert_keys to cover the first unique index as follows:
upsert_keys
class Foo < ActiveRecord::Base upsert_keys [:a, :b, :c] end
However, whenever we do an upsert on a record that has a nil value for c, we run into duplicate key errors from the second unique index.
upsert
nil
c
Is there any way in the current library to set the index_column_names and index_expression per upsert call?
index_column_names
index_expression
For example, something like:
if foo.c.nil? foo.upsert(upsert_keys: [:a, :b], :upsert_options: {where: "c IS NULL"}) else foo.upsert(upsert_keys: [:a, :b, :c]) end
Would like to bump this, as I'm requiring this feature for something I'm working on as well.
It's not uncommon to have different upsert clauses.
Pull Requests are welcome!
@olleolleolle just added one 👍
We have a
foo
table that has two unique indices:In our model, we specify the
upsert_keys
to cover the first unique index as follows:However, whenever we do an
upsert
on a record that has anil
value forc
, we run into duplicate key errors from the second unique index.Is there any way in the current library to set the
index_column_names
andindex_expression
perupsert
call?For example, something like: