INSERT INTO `users` (`age`, `email`, `updatedDate`)VALUES (?, ?, NOW())
Multiple rows can be inserted in a batch by passing an array of structs to insert.
This is not the same as looping over and array and calling insert in the loop. Using an array with insert will batch the inserts in one SQL call. Looping over an array and calling insert each time will create a SQL request for each item in the array. Bottom line, pass your array to insert!
A single column, a list or columns, or an array of columns to return from the inserted query.
returning is only supported in PostgresGrammar and SqlServerGrammar. Using this method on unsupported grammars will result in an UnsupportedOperation exception. Be aware that using this method constrains your grammar choices.
Specifies columns to be returned from the insert query.
INSERT INTO `users` (`email`, `name`)VALUES (?, ?)
upsert
Name
Type
Required
Default
Description
values
struct | array<struct>
true
A struct or array of structs to insert into or update on the table.
target
string | array<string>
true
A column name or array of column names to match the values to the table. If a match is found, the record will be updated. Otherwise, a new record will be inserted. Most database grammars required these columns to have either a primary key or a unique index.
update
array | struct
false
null
Either an array of columns to update using the current value matched or a struct containing the column names as keys and the corresponding to update. If blank, it will update all the columns in the passed in value.
options
boolean
false
{}
Any additional queryExecute options.
toSql
boolean
false
false
If true, returns the raw SQL string instead of running the query. Useful for debugging.
An upsert is a batch operation that either inserts or updates a row depending on if a target match is found. If a row is matched with the target column(s), then the matched row is updated. Otherwise a new row is inserted.
In most database grammars, the target columns are required to be primary key or unique indexes.
MERGE INTO "USERS""QB_TARGET"USING ( SELECT ?, ?, ?, ? FROM dual UNION ALL SELECT ?, ?, ?, ? FROM dual) "QB_SRC"ON"QB_TARGET"."USERNAME"="QB_SRC"."USERNAME"WHEN MATCHED THEN UPDATE SET "ACTIVE"="QB_SRC"."ACTIVE","MODIFIEDDATE"="QB_SRC"."MODIFIEDDATE"WHEN NOT MATCHED THEN INSERT ("ACTIVE", "CREATEDDATE", "MODIFIEDDATE", "USERNAME")VALUES ("QB_SRC"."ACTIVE", "QB_SRC"."CREATEDDATE", "QB_SRC"."MODIFIEDDATE", "QB_SRC"."USERNAME")
The update clause in a upsert can also accept raw values, making it very useful for tracking data like statistics.
MERGE [stats] AS [qb_target]USING (VALUES (?, ?, ?), (?, ?, ?)) AS [qb_src] ([postId], [viewedDate], [views])ON [qb_target].[postId] = [qb_src].[postId] AND [qb_target].[viewedDate] = [qb_src].[viewedDate]WHEN MATCHED THEN UPDATE SET [views] = stats.views +1WHEN NOT MATCHED BY TARGET THEN INSERT ([postId], [viewedDate], [views])VALUES ([postId], [viewedDate], [views])
INSERT INTO "stats" ("postId", "viewedDate", "views")VALUES (?, ?, ?), (?, ?, ?)ON CONFLICT ("postId", "viewedDate") DO UPDATE"views"= stats.views +1
MERGE INTO "STATS""QB_TARGET"USING ( SELECT ?, ?, ? FROM dual UNION ALL SELECT ?, ?, ? FROM dual) "QB_SRC"ON"QB_TARGET"."POSTID"="QB_SRC"."POSTID" AND "QB_TARGET"."VIEWEDDATE"="QB_SRC"."VIEWEDDATE"WHEN MATCHED THEN UPDATE SET "VIEWS"= stats.views +1WHEN NOT MATCHED THEN INSERT ("POSTID", "VIEWEDDATE", "VIEWS")VALUES ("QB_SRC"."POSTID", "QB_SRC"."VIEWEDDATE", "QB_SRC"."VIEWS")
delete
Name
Type
Required
Default
Description
id
any
false
A convenience argument for `where( "id", "=", arguments.id ). The query can be constrained by normal WHERE methods as well.
idColumn
string
false
"id"
The name of the id column for the delete shorthand.
options
boolean
false
{}
Any additional queryExecute options.
toSql
boolean
false
false
If true, returns the raw SQL string instead of running the query. Useful for debugging.