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

448
Vistas
Prisma, update scalarList/array

I´m doing an Spotify clone and I´m trying to add a song to a playlist but my query doesn't work, until this point, everything was good following the documentation on prisma docs, but I cannot do this query, every time get an error, so if someone can tell me, how can I do this with an example, I'll be very grateful.

My question is, having this schema, how can I add a song to a playlist? there are two models affected by the query, song (where i am trying to add) and playlist.

My schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}

model User {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  email     String    @unique
  firstName String
  lastName  String
  password  String
  playlists Playlist[]
}

// here
model Song {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  name      String
  artist    Artist    @relation(fields: [artistId], references: [id])
  artistId  Int
  playlists Playlist[]
  duration  Int
  url       String
}

model Artist {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  songs     Song[]
  name      String    @unique
}

// here
model Playlist {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  name      String
  songs     Song[]
  user      User      @relation(fields: [userId], references: [id])
  userId    Int
}

I am trying to add the song like this:

let songId = 1;
let playlistId = 1;
let lists;

// get the playlists the song is part of

lists = await prisma.song.findFirst({
        select: {
            playlists: true
        },
        where: {
            id: +songId
        }
    })

// get the playlist data i need
const list = await prisma.playlist.findUnique({
       where: {
            id: playlistId
         }
       })

// create the array for update with the data
// plus the data I want to add

lists = [ ...lists.playlists, list ]

// trying to update the old array with the new data (lists)
// this is what i'm doing wrong, help please

await prisma.song.update({
     where: { id: +songId },
     data:{
         playlists: lists

        }
     })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

after many tries i finally got what i want if anyone knows a better way please tell me, i want to learn, for now this is my solution:

i need to send each value as id: playlistId

const song = await prisma.song.findUnique({
    select: {
       playlists: true
     },
     where: {
        id: +songId
     }
})

//   get an array of objects, id: playlistId
const songPlaylistsIds = song.playlists.map( playlist => ({id: playlist.id}))

//   I prepare the array with the content that already exists plus the new content that I want to add:
const playlists =  [...songPlaylistsIds, { id: playlistId}]

await prisma.song.update({
       where: { id: +songId },
       data:{
          playlists: {
             // finally for each object in the array i get, id: playlistId and it works.
             set: playlists.map( playlistSong => ({ ...playlistSong })) 
          }                
       }
})

Problems I had doing this: I was wrong in thinking that it should work as simple as playlist: lists I wanted to change the content to a new one but I couldn't, I needed to send the values ​​one by one. Another error when I get the content of the playlists. I had the full object but just needed to send the id. And lastly, in the prisma documentation, there is a method like set, push, but this method doesn't work, at least I don't know how to make push work

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