module Sequel::Plugins::InsertConflict

  1. lib/sequel/plugins/insert_conflict.rb

The insert_conflict plugin allows handling conflicts due to unique constraints when saving new model instance, using the INSERT ON CONFLICT support in PostgreSQL 9.5+ and SQLite 3.24.0+. Example:

class Album < Sequel::Model
  plugin :insert_conflict
end

Album.new(name: 'Foo', copies_sold: 1000).
  insert_conflict(
    target: :name,
    update: {copies_sold: Sequel[:excluded][:b]}
  ).
  save

This example will try to insert the album, but if there is an existing album with the name ‘Foo’, this will update the copies_sold attribute for that album. See the PostgreSQL and SQLite adapter documention for the options you can pass to the insert_conflict method.

You should not attempt to use this plugin to ignore conflicts when inserting, you should only use it to turn insert conflicts into updates. Any usage to ignore conflicts is not recommended or supported.

Usage:

# Make all model subclasses support insert_conflict
Sequel::Model.plugin :insert_conflict

# Make the Album class support insert_conflict
Album.plugin :insert_conflict

Methods

Public Class

  1. configure

Public Class methods

configure(model)
[show source]
   # File lib/sequel/plugins/insert_conflict.rb
37 def self.configure(model)
38   model.instance_exec do
39     if @dataset && !@dataset.respond_to?(:insert_conflict)
40       raise Error, "#{self}'s dataset does not support insert_conflict"
41     end
42   end
43 end