Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Creating indirect references with Prisma

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think an Interactive Transaction would work here.

Simply create a transaction where:

  1. Create the "creator" or "user" record first.
  2. Use the returned object from the "creator" record and reference its id as you please when creating the "posts"/dependent record.

1 and 2 are separate Prisma queries here (not a single nested write).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!