`createMany()` should return the created records
Problem
Initially brought up here: https://github.com/prisma/prisma/issues/4998#issuecomment-787206842 Problem The fact that the `createMany` API does not return the created records (which should be possible for Postgres) makes it difficult/impossible to use in some circumstances, meaning we must fall back to raw SQL. Here's an example. Let's say I have these two tables [code block] Let's say I want to create many posts and many comments. The data to be created could be represented like so: `Array<{post: PostData, comments: Array<CommentData>}>`. If `createMany` returned the created data, I could do something like this: [code block] However, since `createMany` does not return the created data, it is difficult to create many posts and then create many comments linked to those posts in an efficient manner. The remaining options are to create records one-by-one or use raw SQL. Using a combination of `createMany` and `findMany` is possible if you manually specify IDs, but does not work if you rely on Postgres (or some other DB) to generate IDs by default. Suggested solution `createMany` should return an array of created records (like how `create` returns the single created record). Alternatives Use `create` (inefficient if creating many records) Use raw SQL (efficient but inconvenient) * Use `createMany` + `findMany` (only works if you manually specify IDs, and also less efficient than if `createMany` returned created records) Additional context Personally, I've used raw
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Enhance `createMany()` to Return Created Records
The current implementation of the `createMany()` API in Prisma does not return the created records, which limits its usability in scenarios where the created records' IDs are needed for subsequent operations. This is particularly problematic for databases like Postgres that can auto-generate IDs, as users cannot efficiently link related records without first retrieving the generated IDs.
Awaiting Verification
Be the first to verify this fix
- 1
Modify `createMany()` Implementation
Update the `createMany()` function in the Prisma client to include a return statement that fetches and returns the created records after insertion. This can be achieved by executing a RETURNING clause in the SQL query for Postgres.
typescriptasync function createManyWithReturn(data) { return await prisma.post.createMany({ data, skipDuplicates: true, select: { id: true, title: true, content: true } }); } - 2
Update Documentation
Revise the Prisma documentation to reflect the new behavior of `createMany()`, including examples of how to utilize the returned records effectively. Ensure that users understand the implications of using this feature with different databases.
markdown### Example Usage const createdPosts = await createManyWithReturn(postsData); console.log(createdPosts); // Logs the array of created posts with IDs - 3
Implement Tests for New Behavior
Create unit tests to verify that the modified `createMany()` function correctly returns the created records. Ensure tests cover various scenarios, including inserting multiple records and handling duplicates.
typescripttest('createMany returns created records', async () => { const postsData = [{ title: 'Post 1', content: 'Content 1' }, { title: 'Post 2', content: 'Content 2' }]; const createdPosts = await createManyWithReturn(postsData); expect(createdPosts.length).toBe(postsData.length); expect(createdPosts[0]).toHaveProperty('id'); }); - 4
Deploy Changes to Production
Once the implementation and testing are complete, deploy the changes to the production environment. Monitor the application for any issues related to the new `createMany()` behavior.
bashnpm run deploy
Validation
To confirm the fix worked, execute the modified `createMany()` function with a sample dataset and verify that the returned records include the expected properties, such as auto-generated IDs. Additionally, run the unit tests to ensure all scenarios pass successfully.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep