redwoodjs/learn.redwoodjs.com

Comment model need "onDelete: Cascade"

snettah opened this issue · 13 comments

https://learn.redwoodjs.com/docs/tutorial2/adding-comments-to-the-schema/#adding-the-comment-model

The relation with Post need onDelete: Cascade to work properly

model Comment {
  id        Int      @id @default(autoincrement())
  name      String
  body      String
  post      Post     @relation(fields: [postId], references: [id], onDelete: Cascade)
  postId    Int
  createdAt DateTime @default(now())
}

Hmm, let me think about this one. I'm not too thrilled with the idea of getting into descriptions of referential integrity in this tutorial, it's meant to be a pretty high-level overview of things, focusing on building and integrating components at this point. I assume that trying to delete a post is failing? I don't think we demonstrate doing that at this point (or later) in the tutorial, but I if someone goes off-script...

If we follow the tutorial we'll have this error redwoodjs/redwood#3680 (comment)

You get that when running the test suite? We've released a patch that should have fixed that issue. Or are you seeing this error somewhere else?

You get that when running the test suite? We've released a patch that should have fixed that issue. Or are you seeing this error somewhere else?

I don't know when have you release the patch, I've got this error yesterday when I run the tests suit

I'm seeing the same problem. The error happens when running the default 'returns all comments' test in comments.test.js.

Here's the exact text of the error:

Invalid `prisma.executeRaw()` invocation:


      Raw query failed. Code: `23503`. Message: `update or delete on table "Post" violates foreign key constraint "Comment_postId_fkey" on table "Comment"

------------------------------------ EDIT ------------------------------

I upgraded via yarn redwood upgrade and I can confirm that this error is fixed (without any schema modification) in version 0.41.0

@snettah have you tried upgrading to the latest version of RedwoodJS? That solved the problem for me.

@cannikin we should probably update the example tutorial referenced in the Tutorial II Prerequisites to be the latest version of RedwoodJS.

Excerpt from current example tutorial package.json file:

  "devDependencies": {
    "@redwoodjs/core": "^0.38.0"
  },

This means that if the reader doesn't run yarn redwood upgrade, by default they'll be using a version of RedwoodJS in which this test will error.

Yep, tutorial updates for v1.0 are in progress as we speak! #173

Actually, I don't think this is an issue with the documentation in Tutorial I. This problem comes up in Tutorial II when the reader starts with this cloned repository: https://github.com/redwoodjs/redwood-tutorial. Am I missing something?

Ahhh that could be it—we haven't updated that repo in a while and it may be missing the fix for the test deletes! I'll create an issue to update. Looks like you already did! redwoodjs/redwood-tutorial#17

Thanks to this PR redwoodjs/redwood-tutorial#17 this should be closed! Feel free to re-open if you've tried starting from this point (or updated Redwood to the latest version in your tutorial app). Thanks for the fix @davidlcorbitt!