The Prisma documentation contains an example on how to create nested records (shortened):
const user = await prisma.user.create({
data: {
email: 'yvette@prisma.io',
name: 'Yvette',
posts: {
create: [
{
title: 'How to make an omelette',
categories: {
create: {
name: 'Easy cooking',
},
},
},
],
},
},
})
Lets say I also need to reference the created user from the created category ("creator" or so). Prisma won't fill in the user-id as it does with direct references, but I also do not have the user-id to specifiy it myself in the data. If I understand Prisma-transactions correctly, I can also not create the records first without the reference from the category to the user, and add the missing reference afterwards but within the same transaction.
So how do you do something like this (my use case is slighty different but comparable) in a safe way?
I think an Interactive Transaction would work here.
Simply create a transaction where:
id as you please when creating the "posts"/dependent record.1 and 2 are separate Prisma queries here (not a single nested write).