Create
create returns the record actually written and accepts select, include, and omit. Updates are covered in Update, deletes in Delete.
Single Record
ts
const user = await db.user.create({
data: { email: "alice@example.com", name: "Alice" },
});1
2
3
2
3
- An
@autoprimary key is generated by the database and cannot be assigned explicitly; defaults,uuid(),now(), and typed JSON values are applied as in bulk inserts. - Nested relation writes, including
connectandcreate, run in one transaction, see Relation Writes. - A unique key that already exists raises
UNIQUE_CONSTRAINT; useupsertto create or update in one call, see upsert.
createMany
ts
const { count } = await db.user.createMany({
data: [{ email: "a@example.com" }, { email: "b@example.com" }],
skipDuplicates: true,
});1
2
3
4
2
3
4
- Scalar fields only — relations are passed through foreign keys; nested relations and auto-generated IDs are not accepted. Defaults,
uuid(),now(), typed JSON, and soft-delete foreign key checks behave as in a singlecreate. - Uses a multi-row
INSERT, split into batches under a 900-parameter budget; all batches share one transaction, and errors other than skipped unique conflicts roll back every batch. - Empty input returns
{ count: 0 }.countis the number actually inserted; withskipDuplicatesunique conflicts (including duplicates within the batch) are not counted, while other validation and foreign key errors still raise.
createManyAndReturn
ts
const created = await db.user.createManyAndReturn({
data: [{ email: "c@example.com" }],
select: { id: true, email: true },
});1
2
3
4
2
3
4
- Supports
select,include,omit, and virtual fields. - Return order is not guaranteed; sort by a business key yourself. Empty input returns an empty array.
Database Differences
| Capability | PostgreSQL | MySQL / MariaDB |
|---|---|---|
Multi-row INSERT | Supported | Supported |
skipDuplicates | ON CONFLICT DO NOTHING | Per-row, catching only unique conflicts |
createManyAndReturn | RETURNING | Per-row insert and primary-key readback in a transaction |
The MySQL / MariaDB adapter does not use RETURNING: auto-generated primary keys use insertId, other records are read back by their supplied primary key, and MySQL avoids INSERT IGNORE so that invalid values, not-null, and foreign key errors are not swallowed. Unique constraints on soft-deleted models include deleted records, so skipDuplicates never restores them; foreign keys pointing at soft-deleted models must pass referential validation before deduplication, see Soft Delete.
Next Steps
- Update: change existing records, including
upsert - Relation Writes: write related records inside one transaction
- Soft Delete: hide records without deleting them