#Issue Connecting Song to Playlist

5 messages · Page 1 of 1 (latest)

mossy crow
#

This is my code:

    //Connect songs already existing in the DB to this playlist aswell
    await prisma.playlist.update({
      where: { id: newPlaylist.id },
      data: {
        songs: {
          connect: existingSongs.map((song) => {return { id: song.id }}),
        },
      },
    });```

Getting this output: ```newPlaylist.id fbf037cd-a424-4437-8e8b-e8b3b85cf027 existingSongs [
  { id: '7396bfe1-7e75-4167-9f14-6574341dd6ee' },
  { id: 'fec7d79c-f5d4-442f-8b37-ada35fceef66' },
  { id: '459466cf-53f8-4e08-b506-d3e7647e97a3' },
  { id: 'fa17fb13-b57e-4496-9237-0a81d3aecb30' },
  { id: 'd4495cff-f523-43e7-a8ed-c078fe2ae472' },
  { id: '739447da-e1f3-4e16-8c9e-1a9963dac24e' },
  { id: '242a2638-a944-4732-936f-dd914c6a4556' },
  { id: 'c271dd3a-785f-44e7-a935-19bd44db8d78' },
  { id: '14c16502-704b-477c-a970-f6e8686efac3' }
]

Invalid `prisma.playlist.update()` invocation in
  65 //Connect songs already existing in the DB to this playlist aswell
→ 66 await prisma.playlist.update(
The required connected records were not found. Expected 9 records to be connected after connect operation on one-to-many relation 'PlaylistToSongsInPlaylists', found 0.```
#

this is my schema:

  provider = "prisma-client-js"
}

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

model User {
  id                  String     @id @default(uuid())
  spotifyUsername     String     @unique
  spotifyRefreshToken String
  createdAt           DateTime   @default(now())
  updatedAt           DateTime   @updatedAt
  playlists           Playlist[]
}

model Playlist {
  id        String             @id @default(uuid())
  userId    String
  ownedBy   User               @relation(fields: [userId], references: [id])
  spotifyId String? //when null, it means it's the user's liked songs instead of a public playlist
  name      String
  songs     SongsInPlaylists[]
  createdAt DateTime           @default(now())
  updatedAt DateTime           @updatedAt
}

model Song {
  id        String             @id @default(uuid())
  name      String
  artist    String
  spotifyId String             @unique
  playlists SongsInPlaylists[]
}

model SongsInPlaylists {
  id         String   @id @default(uuid())
  songId     String
  playlistId String
  song       Song     @relation(fields: [songId], references: [id])
  playlist   Playlist @relation(fields: [playlistId], references: [id])

  @@unique([songId, playlistId])
}
mossy crow
#

bump

craggy timber
# mossy crow This is my code: ``` console.log("newPlaylist.id", newPlaylist.id, "existing...

Hi vmathi Be sure the song already exist. Since you're using a junction table (SongsInPlaylists), you might need to create new entries in this table instead of connecting existing ones. Here's how you can modify your code:

await prisma.playlist.update({
  where: { id: newPlaylist.id },
  data: {
    songs: {
      create: existingSongs.map((song) => {
        return {
          song: {
            connect: { id: song.id }
          }
        };
      }),
    },
  },
});

Also, be sure the song isn't already connected to the playlist.