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

678
Vistas
Prisma: update nested entities in a single query

I am using Prisma and Express.js to make requests to my MySQL database table.

I have one-to-many relationship between my Contest-Round tables and am trying to write a query which would allow me to differently update rounds for a given contest. Given the schema:

model Contest {
    id            Int      @id @default(autoincrement())
    name          String   @unique
    rounds        Round[]
    ... other fields
}

model Round {
    id        Int       @id @default(autoincrement())
    name      String
    contestId Int
    contest   Contest   @relation(fields: [contestId], references: [id], onDelete: Cascade)
    ... other fields
}

What I want to achieve is to update the contest from

{
    id: 100,
    name: 'contest1',
    rounds: [
        {
             id: 1,
             name: 'round1',
             contestId: 100,
        },
        {
             id: 2,
             name: 'round2',
             contestId: 100,
        }
    ]

to for example

{
    id: 100,
    name: 'contest1',
    rounds: [
        {
             id: 1,
             name: 'round1Updated',
             contestId: 100,
        },
        {
             id: 2,
             name: 'round2UpdatedDifferently',
             contestId: 100,
        }
    ]

where the round names I get from the form value from HTML.

I haven't found any examples on updating different nested entities, so I'm expecting it to be something like this:

     var updated = await prisma.contest.update({
            where: {
                id: 100
            },
            data: {
                name: data.name,
                rounds: {
                    update: {
                        where: {
                            id: { in: [1, 2] },
                        },
                        data: {
                            name: ['round1Updated', 'round2UpdatedDifferently']   
                        },
                    },
                }
            },
            include: {
                rounds: true,
            }
        });

Any ideas or clues would be appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Will something like this work?

const updated = await prisma.contest.update({
  where: { id: 100 },
  data: {
    name: data.name,
    rounds: {
      deleteMany: { id: { in: [1, 2] } }, // Delete existing records first
      createMany: { // Update by creating new records
        data: [
          { id: 1, name: "round1Updated" },
          { id: 2, name: "round2UpdatedDifferently" },
        ]
      }
    }
  },
  include: { rounds: true }
});

From form data

const updated = await prisma.contest.update({
  where: { id: data.id },
  data: {
    name: data.name,
    rounds: {
      deleteMany: { id: { in: data.rounds.map(({ id }) => id) } },
      createMany: { data: rounds }
    }
  },
  include: { rounds: true }
});

Note that the ordering of operations do matter:
https://github.com/prisma/prisma/discussions/6263

about 4 years ago · Juan Pablo Isaza Denunciar

0

const updated = await prisma.contest.update({
  where: {id: data.id},
  data: {
    name: data.name,
    rounds: {
      deleteMany: {},
      createMany: {data: rounds},
    }
  },
  include: {rounds: true},
});
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