Ok I found that this was actually quite simple... just do it as usual insert and include the forign key id.
const result = await prisma.posts.createMany([
{
...postData,
userId,
},
]);
Answer from fotoflo on Stack OverflowOk I found that this was actually quite simple... just do it as usual insert and include the forign key id.
const result = await prisma.posts.createMany([
{
...postData,
userId,
},
]);
This better works on my end. https://www.prisma.io/docs/orm/reference/prisma-client-reference#create
async function main() {
let users: Prisma.UserCreateInput[] = [
{
email: '[email protected]',
userId: { connect: { id }
},
{
email: '[email protected]',
userId: { connect: { id }
},
]
await Promise.all(
users.map(async (user) => {
await prisma.user.create({
data: user,
})
})
)
}
Prisma CreateMany with a relation (connect)
is it possible to have a connect clause within a createMany operation?
How to createMany with a relation? Looping CREATE and getting "too many connections"?
How to use connect inside createMany ?
What you need here is connectOrCreate.
So something like this should work:
await prisma.post.create({
data: {
title: 'Hello',
categories: {
create: [
{
category: {
create: {
name: 'category-1',
},
},
},
{ category: { connect: { id: 10 } } },
],
},
},
});
You can also read more about this in the docs here
After hours of trying I finally came up with the following solution for my use case.
park[] <-> exercise[]
// create parks
const parks = await prisma.park.createMany({data: [...]});
// create and connect exercises
const exercises = [...];
await Promise.all(
exercises.map(async (exercise) => {
await prisma.exercise.create({
data: <any>{
...exercise,
parks: {
connect: parks.map((park) => ({ id: park.id })),
},
},
});
}),
);
I want to be able to use prisma createMany in a nested object. Is this possible? Basically a deck has many sets and sets have many cards. I want to be able to create this all at once.
Like this:
const deck = await context.prisma.deck.create({
data: {
sets: { createMany: { data: [...newSets, cards: { createMany: {data: [...newCards]}}] } },
},
});Ahh I see. just found this. createMany is not supported for SQLite.
createMany is not supported on SQLite unfortunately: #10710 Documented here: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#remarks-10
https://github.com/prisma/prisma/issues/11507#issuecomment-1025587202
Update: Prisma now supports createMany on SQLite, see https://www.prisma.io/docs/orm/reference/prisma-client-reference#createmany
However, note that:
You cannot create or connect relations by using nested create, createMany, connect, connectOrCreate queries inside a top-level createMany() query.
If this is why you aren't seeing the function, there's good news:
You can use a nested a createMany query inside an update() or create() query - for example, add a User and two Post records with a nested createMany at the same time.
