I’m facing a dilemma.
I’ve created a model with an wave_order
column with uniqueness db index with savable
. wavable
is a polymorphic (but we don’t care actually).
# migration
create_table :invitation_waves do |t|
t.string :name, null: false
t.references :wavable, polymorphic: true, index: true
t.integer :wave_order, null: false
t.timestamps
end
add_index :invitation_waves, [:wavable_id, :wavable_type, :wave_order], unique: true
# This first time, it's working
InvitationWave.upsert_all([{id: 1, name: "Wave1", wave_order: 0, wavable_id: my_object.id, wavable_class: my_object.class.name}, {id: 2, name: "Wave2", wave_order: 1, wavable_id: my_object.id, wavable_class: my_object.class.name}], unique_by: :id, update_only: :wave_order, record_timestamps: true)
# The second time, it's not because of the db constraint.
InvitationWave.upsert_all([{id: 1, name: "Wave1", wave_order: 1, wavable_id: my_object.id, wavable_class: my_object.class.name}, {id: 2, name: "Wave2", wave_order: 2, wavable_id: my_object.id, wavable_class: my_object.class.name}], unique_by: :id, update_only: :wave_order, record_timestamps: true)
Is there any other solution than those 2:
- Remove the db index uniqueness
- Update manually each row