I have the following model in my schema with CASCA...
# orm-help
r
I have the following model in my schema with CASCADE set:
Copy code
model Solve {
  user   User @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId Int

  challenge   Challenge @relation(fields: [challengeId], references: [id], onDelete: Cascade)
  challengeId Int

  createdAt DateTime @default(now()) @map(name: "created_at")

  @@id([userId, challengeId])
}
r
@Robin 👋 Works fine for me with dummy data. Have you added
referentialActions
to
previewFeatures
in your
schema.prisma
?
r
yes I did. then I ran npx prisma generate and npx prisma db push
I can see in the sqlite file that the cascade worked
Copy code
CREATE TABLE IF NOT EXISTS "Challenge" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "title" TEXT NOT NULL,
    "text" TEXT NOT NULL,
    "solution" TEXT NOT NULL,
    "points" INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS "Solve" (
    "userId" INTEGER NOT NULL,
    "challengeId" INTEGER NOT NULL,
    "created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,

    PRIMARY KEY ("userId", "challengeId"),
    FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY ("challengeId") REFERENCES "Challenge" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
and this is the code I am running
Copy code
await prisma.user.delete({
    where: {
      id: session.userId as number,
    },
  });
r
Yeah I get the same and on adding sample data and then performing a delete, it works fine:
Copy code
`await prisma.user.create({
    data: {
      name: 'user1',
      solves: { create: { challenge: { create: { name: 'ch1' } } } },
    },
  })

  await prisma.user.create({
    data: {
      name: 'user1',
      solves: { create: { challenge: { connect: { id: 1 } } } },
    },
  })

  await prisma.user.delete({ where: { id: 1 } })
I would check if any of your relations are messed up or not.
Otherwise I would need a sample reproduction.
r
it worked now, sorry for taking your time! it looks like I needed to restart the dev server
it did not pick up the changes with auto reloading
I'll remember next time restarting everything
r
Are you using
ts-node
or
nodemon
? If so, then it should pick up the changes.
r
I'm running next.js with next run dev