Open
Description
Hi,
Today, I was checking the docs regarding createManyAndReturn
and I saw the following example.
https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#using-nested-createmany
const categories = await prisma.category.createManyAndReturn({
data: [
{ name: 'Fun', },
{ name: 'Technology', },
{ name: 'Sports', }
],
select: {
id: true
}
});
const posts = await prisma.post.createManyAndReturn({
data: [{
title: "Funniest moments in 2024",
categoryId: categories[0].id
}, {
title: "Linux or macOS — what's better?",
categoryId: categories[1].id
},
{
title: "Who will win the next soccer championship?",
categoryId: categories[2].id
}]
});
This example gives the impression that the returned categories order is matching the input order, even though this is not always the case (Check this discussion here prisma/prisma#24894)
My suggestion is the following, we can update the workaround example like this
const categories = await prisma.category.createManyAndReturn({
data: [
{ name: 'Fun', },
{ name: 'Technology', },
{ name: 'Sports', }
],
select: {
id: true
}
});
const posts = await prisma.post.createManyAndReturn({
data: [{
title: "Funniest moments in 2024",
categoryId: categories.filter(category => category.name === 'Fun')!.id
}, {
title: "Linux or macOS — what's better?",
categoryId: categories.filter(category => category.name === 'Technology')!.id
},
{
title: "Who will win the next soccer championship?",
categoryId: categories.filter(category => category.name === 'Sports')!.id
}]
});
Also, I believe that the fact that the order is not garanteed should be mentioned in the remarks of createManyAndReturn
section
https://www.prisma.io/docs/orm/reference/prisma-client-reference#createmanyandreturn