Single-row creation
Create a single record from validated application input.
Drizzle ORM · Module
Inserts one or more typed rows into a Drizzle table with an SQL-like builder. The same page also covers parameterization, insert-from-select, returned rows, and conflict-handling forms.
Call insert with a table, then provide either one object or an array of objects to values. The inferred insert type checks required and optional columns, while conflict handlers and returning can extend the operation where the dialect supports them.
db.insert(table).values(values)| Name | Type | Description |
|---|---|---|
table | Table | The table receiving the new row or rows. |
values | InsertValue | InsertValue[] | One insert object or an array of insert objects; values may also be supplied from a select form documented on the page. |
onConflictDoUpdate | Conflict configuration | Optional conflict target and update configuration for PostgreSQL, CockroachDB, and SQLite. |
A dialect-specific insert query builder; awaiting it executes the insert and resolves with the dialect's result, or with returned rows when returning is supported and requested.
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';
import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core';
const users = sqliteTable('users', {
id: integer('id').primaryKey(),
name: text('name').notNull(),
});
const db = drizzle(new Database(':memory:'));
await db.insert(users).values({ id: 1, name: 'Ada' });
await db.insert(users).values([
{ id: 2, name: 'Lin' },
{ id: 3, name: 'Mina' },
]);Creates one row and then inserts two rows with the SQLite Drizzle query builder.
Create a single record from validated application input.
Persist several records efficiently in one insert operation.
Populate a table from the result of a select query.
Ignore or update rows that violate a uniqueness constraint.
db.insert(table) starts an insert query, and values accepts either one row object or an array for multiple rows. Drizzle generates the SQL and parameterizes the supplied values.
Follow-up: How would you avoid failing on a duplicate key? Use onConflictDoNothing, optionally supplying a conflict target.
Define a type with typeof users.$inferInsert, then pass a value matching that inferred insert shape to values. This keeps application data aligned with the table schema.
Follow-up: What should be checked when a field is omitted? Verify whether the schema supplies a default or marks the field as optional for inserts.
For PostgreSQL, CockroachDB, and SQLite, onConflictDoUpdate updates a conflicting row using a target and set object. The page also documents conditional targetWhere and setWhere clauses for more precise conflict handling.
Follow-up: How does MySQL differ? Its insert upsert form uses onDuplicateKeyUpdate rather than onConflictDoUpdate.
$inferInsertvaluesreturningonConflictDoNothingonConflictDoUpdatesetWheretargetWhereonDuplicateKeyUpdate