Hello, I'm porting a side project from Prisma 1 to...
# orm-help
n
Hello, I'm porting a side project from Prisma 1 to Prisma 2 and I'm trying to get a self referential many-to-many relationship working. The model looks like -
Copy code
model User {
  id        Int @id @default(autoincrement())
  name      String
  email     String @unique
  friendedBy  User[]   @relation("Friends", references: [id])
  friended  User[]   @relation("Friends", references: [id])
}
And the generated postgres tables look like -
Copy code
CREATE TABLE public."User" (
  email text NOT NULL,
  name text NOT NULL,
  id integer NOT NULL
);
ALTER TABLE
  public."User"
ADD
  CONSTRAINT "User_pkey" PRIMARY KEY (id)

CREATE TABLE public."_Friends" ("B" integer NOT NULL, "A" integer NOT NULL);

CREATE UNIQUE INDEX "_Friends_AB_unique" ON "_Friends"("A", "B");
CREATE INDEX "_Friends_B_index" ON "_Friends"("B");
ALTER TABLE "_Friends" ADD FOREIGN KEY ("A") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "_Friends" ADD FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
The trouble I'm having is adding friends to a user. This is what I'm trying -
Copy code
await prisma.user.update({
    where: {
      id: input.id,
    },
    data: {
      friends: {
        create: { A: input.id, B: input.friendId },
      },
    },
  })
It doesn't work and I suspect I might need a connect in there. Any suggestions?