Yunbo
07/25/2022, 5:49 PMconst u = await prisma.user.create({
data: {
email: 'emma@prisma.io',
posts: {
create: [
{
title: 'My first post',
external_user_id: `a1b2c34d-{user.id}`
}
],
},
},
})
about external_user_id , so how can i get user.id (that is being created in this query) dynamically?Sabin Adams
07/25/2022, 6:03 PMuser and at the same time create a post with the related user’s id as the external_user_id.
Check out the example here. Let me know if that helps!Yunbo
07/25/2022, 6:09 PMSabin Adams
07/25/2022, 6:13 PMconst user = await prisma.user.create({
data: {
email: 'yvette@prisma.io',
posts: {
create: [
{ title: 'How to make an omelette' }
],
},
}
})
You don’t need to worry about populating the external_user_id, it will be filled automatically as long as it is the defined relation field in your Prisma schemaYunbo
07/25/2022, 6:18 PMexternal_user_id is not the same as id
id would be 12 something like that which prisma nicely handles but extenal_user_id contains arbitrary string handler so would look like asdije-12 and this arbitrary handler changes per new post.Sabin Adams
07/25/2022, 6:24 PMconst user = await prisma.user.create({
select: {
id: true,
posts: {
select: {
id: true
}
}
},
data: {
email: 'test@gmail.com',
posts: {
create: {
title: 'Test'
}
}
}
})
await prisma.post.update({
where: { id: user.posts[0].id },
data: { external_user_id: `asd-${user.id}`}
})
There are of course safeguards you’d want to put in place to ensure you’re updating the correct post and the post actually exists, but that’s the general gistYunbo
07/25/2022, 6:34 PMYunbo
07/25/2022, 6:35 PMSabin Adams
07/25/2022, 6:37 PM