prismock
prismock copied to clipboard
Self Referencing Relation returns all Members
Hello, I am using the following schema for users in my app,
model User {
id Int @id @default(autoincrement())
...
// Friends
// If a User is in requested and requesting then they are considered friends
requested User[] @relation("friends")
requesting User[] @relation("friends")
...
}
When I attempt to retrieve either of these relations through include I get every user in the database inside of both lists. i.e. if there are two users, user1 and user2, a request like
const user = await this.userDB.findUnique({
where: { id: userId },
include: {
requested: true,
requesting: true,
},
});
Will have both requested: [ {user1}, {user2} ]
and requesting: [ {user1}, {user2} ]
. No matter if they have been connected or not.
Not sure if this is a bug or just not implemented yet, Thanks
Hello @W1nst0n03 , thanks for the submission, I'll have a look to get that fixed in the next few days
For others with the same issue, you can get around it by explicitly defining the many-to-many self-referencing relationship with another table (which is actually how prisma represents it in the database itself).
Hello @W1nst0n03 , after having a deeper look into this issue, it seems like the problem comes from supporting custom @relation
. I'll add this to my list of to-do but I don't expect to have it fixed short-term.