Noob Question Many-to-Many in prisma after nextAuth

Hello all! I'm trying to create my first many-to-many relationship in Prisma. schema:
model User {
id String @id @default(cuid())
teams usersInTeams[]
}

model Team {
id String @id @default(cuid())
name String @unique
slug String @unique
users usersInTeams[]
}

model usersInTeams {
userId String @db.VarChar(64)
teamId String @db.VarChar(64)
user User @relation(fields: [userId], references: [id])
team Team @relation(fields: [teamId], references: [id])
role String @default("USER")

@@id([userId, teamId])
}
model User {
id String @id @default(cuid())
teams usersInTeams[]
}

model Team {
id String @id @default(cuid())
name String @unique
slug String @unique
users usersInTeams[]
}

model usersInTeams {
userId String @db.VarChar(64)
teamId String @db.VarChar(64)
user User @relation(fields: [userId], references: [id])
team Team @relation(fields: [teamId], references: [id])
role String @default("USER")

@@id([userId, teamId])
}
export async function createTeam({ teamName, ownerId }: CreateTeamProps) {
try {
const team = await prisma.team.create({
data: {
name: teamName,
users: {
connect: [
{
userId_teamId: // the only type in intellesense, but it depends on a created team?
},
],
},
},
});

return team;
} catch (error) {
console.error(error);
return null;
}
}
export async function createTeam({ teamName, ownerId }: CreateTeamProps) {
try {
const team = await prisma.team.create({
data: {
name: teamName,
users: {
connect: [
{
userId_teamId: // the only type in intellesense, but it depends on a created team?
},
],
},
},
});

return team;
} catch (error) {
console.error(error);
return null;
}
}
but i cant for the life of me figure out how to write the createTeam() function - this is as close as i've gotten...
20 Replies
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
yeah i thought so too!
fotoflo
fotofloOP•2y ago
fotoflo
fotofloOP•2y ago
@iynaix i wonder whats up
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Brendonovich
Brendonovich•2y ago
I think your problem is that you're using connect where you should be using create. You're creating a new team for an existing user, so you're also going to create a new relation record. Using connect like you have requires providing an ID to lookup, but that's not possible since no record exists yet and you need to create it.
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
just got offa. call, let me see
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
it compiled, but jest is giving me some trouble... will check it in a min hopefully Invalid prisma.team.create() invocation:

Foreign key constraint failed on the field: userId trying the second one ( which is closer to what i throught it should be ) Invalid prisma.team.create() invocation:

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 'usersInTeams' record(s)) was found for a nested connect on one-to-many relation 'UserTousersInTeams'. seems correct let me check my test looks like the right kind of error 🙂 @iynaix! it worked! thank you users: creates the user join table for the user and then connects the user is that the correct understanding?
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
model usersInTeams {
userId String @db.VarChar(64)
teamId String @db.VarChar(64)
user User @relation(fields: [userId], references: [id])
team Team @relation(fields: [teamId], references: [id])
role String @default("USER")

@@id([userId, teamId])
}
model usersInTeams {
userId String @db.VarChar(64)
teamId String @db.VarChar(64)
user User @relation(fields: [userId], references: [id])
team Team @relation(fields: [teamId], references: [id])
role String @default("USER")

@@id([userId, teamId])
}
hmm where do i put the role?
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
const team = await prisma.team.create({
data: {
name: teamName,
plan: "free",
slug: teamName,
users: {
create: [
{
user: {
connect: {
id: ownerId,
},
},
role: "owner",
},
],
},
},
});
const team = await prisma.team.create({
data: {
name: teamName,
plan: "free",
slug: teamName,
users: {
create: [
{
user: {
connect: {
id: ownerId,
},
},
role: "owner",
},
],
},
},
});
@iynaix haha i read that question too but i wasnt smart enough to extract an answer thank you! big help
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
now next question @iynaix if you know it hah
fotoflo
fotofloOP•2y ago
fotoflo
fotofloOP•2y ago
in [..nextauth] - where is the database query for the user? can i get it to add more info to the user?
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
fotoflo
fotofloOP•2y ago
thanks then, its gone a long way alrady!
Want results from more Discord servers?
Add your server