P
Prisma3mo ago
seromine

[Prisma] Nested create with 2 levels of related records

Say I have an object:
const params = {
name: 'bob',
age: '25',
posts: [
{
body: 'hello',
uploads: [
'cat.jpg',
'dog.jpg',
],
},
{
body: 'test',
uploads: [
'meme.jpg',
'pic.jpg',
]
},
],
}
const params = {
name: 'bob',
age: '25',
posts: [
{
body: 'hello',
uploads: [
'cat.jpg',
'dog.jpg',
],
},
{
body: 'test',
uploads: [
'meme.jpg',
'pic.jpg',
]
},
],
}
How do I create/update such an object with Prisma? Nested creates are not possible, so 'posts' and 'uploads' has to be created separately. But how do I create 'uploads' which is an array within in array?
2 Replies
Nik
Nik3mo ago
model User {
name String
age String
posts Post[]
}

model Post {
body String
uploads String[]

user User @relation(fields: [user_id], references: [id])
user_id String
}
model User {
name String
age String
posts Post[]
}

model Post {
body String
uploads String[]

user User @relation(fields: [user_id], references: [id])
user_id String
}
let user = await prisma.user.create({data:{ ... }})
await prisma.post.create({data:{ ..., user_id:user.id }})
let user = await prisma.user.create({data:{ ... }})
await prisma.post.create({data:{ ..., user_id:user.id }})
seromine
seromine3mo ago
Thanks alot @Nik , I'll try this out tomorrow