Skip to content

createManyAndReturn return order #6746

Open
@alouini333

Description

@alouini333

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation creation, updates or corrections

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions