Nick
03/29/2021, 11:00 PMmodel 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 -
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 -
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?