mapping a column in model to another model

I have nextauth with github I want github usernames to be fetched so I can display them alongside comments. working state of my schema.prisma:
model Session {
id String @id @default(cuid())
sessi onToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
comments Comment[]
}

model VerificationToken {
identifier String
token String @unique
expires DateTime

@@unique([identifier, token])
}

model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
content String @db.Text
user User @relation(fields: [userId], references: [id])

}
model Session {
id String @id @default(cuid())
sessi onToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
comments Comment[]
}

model VerificationToken {
identifier String
token String @unique
expires DateTime

@@unique([identifier, token])
}

model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
content String @db.Text
user User @relation(fields: [userId], references: [id])

}
I think I'm supposed to add another field for a relation between User and Comment tables? see that I added userName
model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
userName String
content String @db.Text
user User @relation(fields: [userId, userName], references: [id, name])
}
model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
userName String
content String @db.Text
user User @relation(fields: [userId, userName], references: [id, name])
}
but I get an error -> Error parsing attribute "@relation": The argument references must refer to a unique criterion in the related model. Consider adding an @@unique([id, name]) attribute to the model User. and I'm kinda lost
1 Reply
Silas | @silaspath
I think I figured it out but will prob break? I defined a compound unique constraint for User id and name:
// ~/schema.prisma

model User {
id String @id @default(cuid())
name String
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
comments Comment[]

@@unique([id, name])
}
// ~/schema.prisma

model User {
id String @id @default(cuid())
name String
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
comments Comment[]

@@unique([id, name])
}
so now my Comment can have two fields that refer to user:
// ~/schema.prisma

model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
userName String
content String @db.Text
user User @relation(fields: [userId, userName], references: [id, name])
}
// ~/schema.prisma

model Comment {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
userName String
content String @db.Text
user User @relation(fields: [userId, userName], references: [id, name])
}
then my session callback is updated to add github user.name to session user.name:
// ~/server/auth.ts
callbacks: {
session({ session, user }) {
console.log(`auth -- user`, user);

if (session.user) {
session.user.id = user.id;
session.user.name = user.name || "";
}
return session;
},
},
// ~/server/auth.ts
callbacks: {
session({ session, user }) {
console.log(`auth -- user`, user);

if (session.user) {
session.user.id = user.id;
session.user.name = user.name || "";
}
return session;
},
},
... however see that logical OR -- I don't want it. I tried to find how to update user type to guarantee it will return name then I had to update my router where it used to just rely on userId. now I have to specify a user object -> create object -> name:
// ~/server/api/routers/guestbook.ts

create: protectedProcedure
.input(z.object({ content: z.string() }))
.mutation(({ ctx, input }) => {
return ctx.prisma.comment.create({
data: {
content: input.content,
user: {
create: {
name: ctx.session.user.name,
},
},
},
});
}),
// ~/server/api/routers/guestbook.ts

create: protectedProcedure
.input(z.object({ content: z.string() }))
.mutation(({ ctx, input }) => {
return ctx.prisma.comment.create({
data: {
content: input.content,
user: {
create: {
name: ctx.session.user.name,
},
},
},
});
}),
if I'm missing some things please let me know!
Want results from more Discord servers?
Add your server