0

I have two models:

model Terminal {
    terminal_id   Int      @id @default(autoincrement())
    terminal_name String
    created_at    DateTime @default(now())
    updated_at    DateTime @updatedAt
    modcods       Modcod[]

    @@map("terminal")
}

model Modcod {
    modcod_id   Int      @id @default(autoincrement())
    name        String?
    created_at  DateTime @default(now())
    updated_at  DateTime @updatedAt
    terminal    Terminal @relation(fields: [terminal_id], references: [terminal_id], onDelete: Cascade, onUpdate: Cascade)
    terminal_id Int

    @@map("modcod")
}

Creating a single resource works just fine:

export async function createSingleModcod(modcod: Modcod, terminal_id: number) {
  const modcod = await prisma.modcod.create({
    data: {
      ...modcod,
      terminal_id, // this is the terminal_id coming from req.body
    },
  });
  await prisma.$disconnect();
  return modcod;
}

Creating many however does not:

export async function createManyModcods(modcods: Modcods[], terminal_id: number) {
  const modcods = await prisma.$transaction(
    modcods.map((modcod) =>
      prisma.modcod.create({
        data: {
          ...modcod,
          terminal_id, // this is the terminal_id coming from req.body
        },
      }),
    ),
  );
  await prisma.$disconnect();
  return modcods;
}

Is this a prisma limitation or am I missing anything?

I've tried the solution above using $transaction and the 'standard' createMany like so:

export async function createManyModcodsAlt(modcods: Modcods[], terminal_id: number) {
  const modcods = await prisma.modcod.createMany({
    data: modcods.map((modcod) => ({
      ...modcod,
      terminal_id,
    })),
  });
  await prisma.$disconnect();
  return modcods;
}

My expectation is that it should work just like creating a single resource

  • Please post the specific error that you're getting. https://stackoverflow.com/help/how-to-ask – Shea Hunter Belsky Dec 01 '22 at 02:33
  • As Shea mentioned, please share the error message. Also, I noticed that you are explicitly disconnecting in your functions There is no need to do that, you should explicitly call $disconnect only in these scenarios: https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/connection-management#calling-disconnect-explicitly If you call $disconnect in every functions, for every subsequent requests PrismaClient would need to reconnect which would increase your response times. – Nurul Sundarani Dec 02 '22 at 06:54

0 Answers0