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

452
Views
Prisma, actualiza scalarList/array

Estoy haciendo un clon de Spotify y estoy tratando de agregar una canción a una lista de reproducción, pero mi consulta no funciona, hasta este punto, todo estaba bien siguiendo la documentación en prisma docs, pero no puedo hacer esta consulta, siempre sale un error, asi que si alguien me puede decir como puedo hacer esto con un ejemplo, se lo agradeceria mucho.

Mi pregunta es, teniendo este esquema, ¿cómo puedo agregar una canción a una lista de reproducción? hay dos modelos afectados por la consulta, la canción (donde estoy tratando de agregar) y la lista de reproducción.

mi esquema:

 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 }

Estoy tratando de agregar la canción de esta manera:

 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 answers
Answer question

0

después de muchos intentos finalmente obtuve lo que quiero si alguien sabe una mejor manera por favor dígame, quiero aprender, por ahora esta es mi solución:

necesito enviar cada valor como 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 })) } } })

Problemas que tuve al hacer esto: Me equivoqué al pensar que debería funcionar tan simple como playlist:lists Quise cambiar el contenido a uno nuevo pero no pude, necesitaba enviar los valores uno por uno. Otro error cuando obtengo el contenido de las listas de reproducción. Tenía el objeto completo pero solo necesitaba enviar la identificación. Y, por último, en la documentación de prisma, hay un método como set, push, pero este método no funciona, al menos no sé cómo hacer que push funcione.

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!