Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda