How do I query self-relations?

I guess my schema must be wrong. But I think I've followed the Prisma docs carefully. I currently have the schema beneath. I am trying to query a user by its id, and include the following and followedBy. But it aint working. I am just receiving the 500 status (from the catch block). The schema
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
following UserFollows[] @relation("follower")
followedBy UserFollows[] @relation("following")
}

model UserFollows {
follower User @relation("follower", fields: [followerId], references: [id])
followerId String
following User @relation("following", fields: [followingId], references: [id])
followingId String

@@id([followerId, followingId])
@@index([followerId])
@@index([followingId])
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
following UserFollows[] @relation("follower")
followedBy UserFollows[] @relation("following")
}

model UserFollows {
follower User @relation("follower", fields: [followerId], references: [id])
followerId String
following User @relation("following", fields: [followingId], references: [id])
followingId String

@@id([followerId, followingId])
@@index([followerId])
@@index([followingId])
}
The query
try {
const data = await prisma.user.findFirst({
where: {
id: id as string
},
include: {
following: true,
}
});

return new Response(JSON.stringify(data));
} catch (error) {
console.error(error);
return new Response("Internal Server Error", { status: 500 });
}
try {
const data = await prisma.user.findFirst({
where: {
id: id as string
},
include: {
following: true,
}
});

return new Response(JSON.stringify(data));
} catch (error) {
console.error(error);
return new Response("Internal Server Error", { status: 500 });
}
4 Replies
SharpieMaster
SharpieMaster15mo ago
wait, isnt following and followed by just a list of users?
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

// Non-Next Auth
tweets Tweet[]
likedTweets Like[]
followers User[] @relation(name: "Followers")
following User[] @relation(name: "Followers")
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

// Non-Next Auth
tweets Tweet[]
likedTweets Like[]
followers User[] @relation(name: "Followers")
following User[] @relation(name: "Followers")
}
I dont think that you need a new model for the follows
Chill Guy
Chill GuyOP15mo ago
That was my initial thought too. And I hope you're right. But then I don't know why the heck they would do it that way too in the docs? Not quite familiar with SQL relations. https://www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations But I still can't include the followers or the following with your schema
Prisma
Self-relations
How to define and work with self-relations in Prisma.
SharpieMaster
SharpieMaster15mo ago
in the dosc it doesnt tell you to create a new model
model User {
id Int @id @default(autoincrement())
name String?
successor User? @relation("BlogOwnerHistory")
predecessorId Int? @unique
predecessor User? @relation("BlogOwnerHistory", fields: [predecessorId], references: [id])
}
model User {
id Int @id @default(autoincrement())
name String?
successor User? @relation("BlogOwnerHistory")
predecessorId Int? @unique
predecessor User? @relation("BlogOwnerHistory", fields: [predecessorId], references: [id])
}
ohh
model User {
id Int @id @default(autoincrement())
name String?
followedBy Follows[] @relation("following")
following Follows[] @relation("follower")
}

model Follows {
follower User @relation("follower", fields: [followerId], references: [id])
followerId Int
following User @relation("following", fields: [followingId], references: [id])
followingId Int

@@id([followerId, followingId])
}
model User {
id Int @id @default(autoincrement())
name String?
followedBy Follows[] @relation("following")
following Follows[] @relation("follower")
}

model Follows {
follower User @relation("follower", fields: [followerId], references: [id])
followerId Int
following User @relation("following", fields: [followingId], references: [id])
followingId Int

@@id([followerId, followingId])
}
try it and see if it works
Chill Guy
Chill GuyOP15mo ago
Worked now with this schema
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

// Non-Next Auth
followedBy User[] @relation("UserFollows")
following User[] @relation("UserFollows")
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

// Non-Next Auth
followedBy User[] @relation("UserFollows")
following User[] @relation("UserFollows")
}
Maybe my client didnt get regenerated properly before when changing the schema. I guess I cold just go with this implicitly. As of right now, I don't think I'll need any other fields for the following/follower stuff.
Want results from more Discord servers?
Add your server