I tried to create track record the have one to many relation with user table and I got the following error.
An operation failed because it depends on one or more records that were required but not found. No 'User' record(s) (needed to inline the relation on 'Track' record(s)) was found for a nested connect on one-to-many relation 'TrackToUser'."
I want who is created this record. So that I added the track to user as following in my schema.
I can create artist and album record.
May I know why I got this issue.
model Genre {
id String @id @default(cuid())
name String @unique @db.VarChar(255)
albums Album[]
@@map("genres")
}
model SongWriter {
id String @id @default(cuid())
name String @unique @db.VarChar(255)
tracks Track[]
@@map("songwriters")
}
model Artist {
id String @id @default(cuid())
name String @unique @db.VarChar(255)
bio String? @db.VarChar(1024)
profile_picture String @db.VarChar(512)
albums Album[]
tracks Track[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("artists")
}
model Album {
id String @id @default(cuid())
title String @db.VarChar(255)
album_cover String @db.VarChar(512)
description String? @db.VarChar(5120)
released Int @default(1900)
artists Artist[]
genres Genre[]
tracks Track[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("albums")
}
model Track {
id String @id @default(cuid())
title String @db.VarChar(255)
description String? @db.VarChar(5120)
mp3_url String @db.VarChar(1024)
youtube_url String @db.VarChar(1024)
duration Int @default(0)
artists Artist[]
album Album @relation(fields: [album_id], references: [id])
album_id String
songWriters SongWriter[]
active Boolean @default(true)
user User @relation(fields: [user_id], references: [id])
user_id String
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("tracks")
}
model User {
id String @id @default(cuid())
name String @unique @db.VarChar(255)
password String @db.VarChar(512)
profile_picture String? @db.VarChar(512)
role Role @default(USER)
tokenVersion Int @default(0)
artists Artist[]
albums Album[]
tracks Track[]
active Boolean @default(true)
created_at DateTime @default(now())
updated_at DateTime? @updatedAt
@@map("users")
}
I have tried to create a Track record and all has worked properly, here you go the code snippet (I do not know if it is what you want):
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const saveData = async () => {
const user = await prisma.user.create({
data: {
name: 'The Best User',
password: '123456',
profile_picture: 'https://www.example.com/user.jpg',
tokenVersion: 1,
}
})
console.log('USER', user)
const gnre = await prisma.genre.create({
data: {
name: 'The Best Genre',
}
})
console.log('GENRE', gnre)
const album = await prisma.album.create({
data: {
title: 'The Best Album',
album_cover: 'https://www.example.com/album.jpg',
description: 'This is the best album ever',
released: 1,
active: true,
user: {
connect: {
id: user.id
}
},
genres: {
connect: {
id: gnre.id
}
}
}
})
console.log('ALBUM', album)
const artist = await prisma.artist.create({
data: {
name: 'The Best Artist',
bio: 'The best artist ever',
profile_picture: 'https://www.example.com/artist.jpg',
active: true,
user: {
connect: {
id: user.id
}
}
}
})
console.log('ARTIST', artist)
const songWriter = await prisma.songWriter.create({
data: {
name: 'The Best Song Writer',
}
})
console.log('SONG WRITER', songWriter)
const track = await prisma.track.create({
data: {
title: 'The Best Track',
description: 'The best track ever',
mp3_url: 'https://www.example.com/track.mp3',
youtube_url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
duration: 1000,
active: true,
artists: {
connect: {
id: artist.id
}
},
album: {
connect: {
id: album.id
}
},
user: {
connect: {
id: user.id
}
},
songWriters: {
connect: {
id: songWriter.id
}
}
},
include: {
artists: true,
album: true,
user: true,
songWriters: true,
}
})
console.log('TRACK')
console.log(JSON.stringify(track, null, 2));
}
saveData()
Here you go the result: