-
hello! Let me see if i got this straight - I have what i believe to be a very simple issue:
Simplified Schema
Now i can create a score like this: async function createScores(batch, data){
return await prisma.scores.create({
data: {
reportingBatch: {
connect: {
id: batch.id
},
},
...data,
},
});
} But i can't use createMany because of the connect call (no create many with "nested relations" - isn't this a simple relation?) so then i have to throw the above in a loop: async function createScoresFromArray(batch, data){
const promises = data.forEach( item => {
return await createScores(batch, item)
});
return await Promise.all(promises)
} Which, beside being sub-optimally performant, runs out of connections before my test suite finishes running... is there a better way to do it? How do i avoid running out of connections? Seems like a simple insert many, with the batchId in the table... am i missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 22 replies
-
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? 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, first a reporting batch is created and then a createMany query on scores model directly sets the foreign key |
Beta Was this translation helpful? Give feedback.
-
In my case I have a many-to-many relation, so I don't have an explicit foreing key (also I'm using Planet Scale so no foreign keys anyway) and I was using
db.collaborator.createMany({
data: collaborator.map((col) => ({
id: ID.Collaborator(),
name: lead.name,
email: lead.email,
// ERROR HERE
// Invalid `prisma.collaborator.createMany()` invocation: ...
// Unknown argument `categories`. Available options are marked with ?
categories: {
connect: lead.categories.map((id) => ({ id })),
},
})),
}); |
Beta Was this translation helpful? Give feedback.
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:
Based on your schema…