3

I know Dataset#multi_insert and Dataset#insert. does multi insert but is there an option available that can skip that particular insert when unique constrain check get activated

shaan
  • 482
  • 5
  • 25

1 Answers1

1

You can use insert_conflict method.

"Handle uniqueness violations when inserting, by updating the conflicting row, using ON CONFLICT. With no options, uses ON CONFLICT DO NOTHING."

DB[:table].insert_conflict.insert(a: 1, b: 2)
# INSERT INTO TABLE (a, b) VALUES (1, 2)
# ON CONFLICT DO NOTHING

It also works with multi_insert

DB[:table].insert_conflict.multi_insert([{a: 1, b: 2}, {a: 3, b: 4}])
# INSERT INTO TABLE (a, b) VALUES (1, 2), (3, 4)
# ON CONFLICT DO NOTHING
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54