Method: Sequel::Dataset#insert

Defined in:
lib/sequel/dataset/actions.rb

#insert(*values) ⇒ Object

Inserts values into the associated table. The returned value is generally the value of the primary key for the inserted row, but that is adapter dependent.

insert handles a number of different argument formats:

  • No arguments, single empty hash - Uses DEFAULT VALUES

  • Single hash - Most common format, treats keys as columns an values as values

  • Single array - Treats entries as values, with no columns

  • Two arrays - Treats first array as columns, second array as values

  • Single Dataset - Treats as an insert based on a selection from the dataset given, with no columns

  • Array and dataset - Treats as an insert based on a selection from the dataset given, with the columns given by the array.

    DB.insert # INSERT INTO items DEFAULT VALUES

    DB.insert({}) # INSERT INTO items DEFAULT VALUES

    DB.insert([1,2,3]) # INSERT INTO items VALUES (1, 2, 3)

    DB.insert([:a, :b], [1,2]) # INSERT INTO items (a, b) VALUES (1, 2)

    DB.insert(:a => 1, :b => 2) # INSERT INTO items (a, b) VALUES (1, 2)

    DB.insert(DB) # INSERT INTO items SELECT * FROM old_items

    DB.insert([:a, :b], DB) # INSERT INTO items (a, b) SELECT * FROM old_items



280
281
282
# File 'lib/sequel/dataset/actions.rb', line 280

def insert(*values)
  execute_insert(insert_sql(*values))
end