prismock icon indicating copy to clipboard operation
prismock copied to clipboard

Self Referencing Relation returns all Members

Open theosiemensrhodes opened this issue 1 year ago • 3 comments

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

theosiemensrhodes avatar Nov 13 '23 02:11 theosiemensrhodes

Hello @W1nst0n03 , thanks for the submission, I'll have a look to get that fixed in the next few days

morintd avatar Nov 16 '23 17:11 morintd

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).

theosiemensrhodes avatar Nov 19 '23 21:11 theosiemensrhodes

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.

morintd avatar Nov 20 '23 20:11 morintd