I feel this is simple but I fear i've just been at it too long I'm starting to not see straight.

I have a parent record with a many to many relationship with a child, such as:
model User {
id String @id @default(cuid())
name String?
username String? @unique

badges UserBadges[]
}

model Badge {
id String @id @default(cuid())
name String @unique
users UserBadges[]
}

model UserBadges {
id String @id @default(cuid())
userId String
badgeId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
badge Badge @relation(fields: [badgeId], references: [id], onDelete: Cascade)
}
model User {
id String @id @default(cuid())
name String?
username String? @unique

badges UserBadges[]
}

model Badge {
id String @id @default(cuid())
name String @unique
users UserBadges[]
}

model UserBadges {
id String @id @default(cuid())
userId String
badgeId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
badge Badge @relation(fields: [badgeId], references: [id], onDelete: Cascade)
}
i want to insert (or upsert). a new User. for that user i will have a list of badges by name only, i will not know the id of them at that point, but they are sure to already exist in the DB.
const user = await prisma.user.upsert({
where: {email: userRecord.email},
update: {
username: userRecord.username,
name: userRecord.name,
badges: {
connect: {
id: userRecord.badges.map((badge: any) => {
return {name: badge.name}
})
}
}
},
create: {
username: userRecord.username,
name: userRecord.name,
badges: userRecord.badges,
}
})
const user = await prisma.user.upsert({
where: {email: userRecord.email},
update: {
username: userRecord.username,
name: userRecord.name,
badges: {
connect: {
id: userRecord.badges.map((badge: any) => {
return {name: badge.name}
})
}
}
},
create: {
username: userRecord.username,
name: userRecord.name,
badges: userRecord.badges,
}
})
around here typescript just yells at me. How do I get this working and connect them by name instead of id?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server