Bulk insertion
Importing or seeding many rows for one model efficiently.
Prisma · Function
createMany() inserts multiple records of the same model in one transactional bulk operation. It returns the number of inserted records and can skip supported duplicate conflicts.
Call createMany() on a model delegate with a data array containing records to insert. The operation runs in a transaction and returns an object containing the number of records created; skipDuplicates can omit rows that violate supported unique constraints.
prisma.<model>.createMany({ data: [...], skipDuplicates?: boolean })| Name | Type | Description |
|---|---|---|
data | Enumerable<ModelCreateManyInput> | An array of objects containing the fields for the records to create. |
skipDuplicates | boolean | Skips records whose supported unique fields conflict with existing records; unsupported by MongoDB, SQL Server, and SQLite. |
A result object containing count, the number of records created.
skipDuplicates option.count field.skipDuplicates with MongoDB, SQL Server, or SQLite.createMany() cannot create or connect related records.const result = await prisma.user.createMany({
data: [
{ name: "Bob", email: "bob@prisma.io" },
{ name: "Yewande", email: "yewande@prisma.io" },
],
skipDuplicates: true,
});
console.log(result.count); // 2Creates multiple User records in one bulk operation and reports how many records were inserted.
Importing or seeding many rows for one model efficiently.
Loading initial application data while avoiding supported unique conflicts.
Writing a set of same-model records where all-or-nothing behavior is desirable.
createMany() return?createMany() accepts a data array and inserts multiple records in a transaction. Its result contains a count property rather than the created records.
Follow-up: How would you avoid duplicate inserts? Use skipDuplicates where the database supports it, or enforce and handle unique constraints explicitly.
createMany() instead of repeated create() calls?It is intended for multiple records of the same model in one bulk operation. Prisma documents bulk writes as transactional, so a failed operation rolls back the entire write.
Follow-up: What limitation applies to relations? A top-level bulk insert cannot perform nested relation creation or connection.
skipDuplicates be used?skipDuplicates tells Prisma to omit records that conflict with supported unique constraints. The option is unavailable for MongoDB, SQL Server, and SQLite.
Follow-up: Does skipping duplicates guarantee every input row is inserted? No; conflicting rows are omitted, so the returned count can be lower than the input length.
createcreateManyAndReturnupdateManydeleteManynested createMany