Skip to content

How to createMany with a relation? Looping CREATE and getting "too many connections"? #17472

Answered by nurul3101
fotoflo asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @fotoflo 👋

If you just need to connect an id, can you try directly setting the foreign key if you are sure that it exists?
Here's an example of what I am suggesting:

import { Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

// A `main` function so that you can use async/await
async function main() {
  await prisma.reportingBatch.create({
    data: {
      id: 1,
    },
  });

  await prisma.scores.createMany({
    data: [
      {
        reportingBatchId: 1,
        score: 1,
        title: "title1",
      },
    ],
  });
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

Based on your schema…

Replies: 2 comments 22 replies

Comment options

You must be logged in to vote
2 replies
@fotoflo
Comment options

@nurul3101
Comment options

Answer selected by fotoflo
Comment options

You must be logged in to vote
20 replies
@divyaswormakai
Comment options

@CodrinSocol
Comment options

@birgersp
Comment options

@mmartinez-hd
Comment options

@Qbason
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet