Method: Sequel::Dataset#insert
- Defined in:
- lib/sequel/dataset/actions.rb
#insert(*values, &block) ⇒ Object
Inserts values into the associated table. The returned value is generally the value of the autoincremented primary key for the inserted row, assuming that a single row is inserted and the table has an autoincrementing primary key.
insert
handles a number of different argument formats:
- no arguments or single empty hash
-
Uses DEFAULT VALUES
- single hash
-
Most common format, treats keys as columns and 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.
Examples:
DB[:items].insert
# INSERT INTO items DEFAULT VALUES
DB[:items].insert({})
# INSERT INTO items DEFAULT VALUES
DB[:items].insert([1,2,3])
# INSERT INTO items VALUES (1, 2, 3)
DB[:items].insert([:a, :b], [1,2])
# INSERT INTO items (a, b) VALUES (1, 2)
DB[:items].insert(a: 1, b: 2)
# INSERT INTO items (a, b) VALUES (1, 2)
DB[:items].insert(DB[:old_items])
# INSERT INTO items SELECT * FROM old_items
DB[:items].insert([:a, :b], DB[:old_items])
# INSERT INTO items (a, b) SELECT * FROM old_items
426 427 428 429 430 431 432 433 |
# File 'lib/sequel/dataset/actions.rb', line 426 def insert(*values, &block) sql = insert_sql(*values) if uses_returning?(:insert) returning_fetch_rows(sql, &block) else execute_insert(sql) end end |