[항해99] 61일차 passport 꼭 끝내고말꺼야는 개뿔 프리즈마했어

IT/Bootcamp 항해99|2021. 8. 7. 09:20

항해99

 

61일차: 

 

38일만 있으면 나도 개발자냐?  우끼고있네 

 

 

 

 

 

오늘은 무슨일이있어도 패스포트를 꼭 끝낼꺼야. 

라고했는데 , 일단 나한테 주어진 일들부터 해야지.

 

마이페이지에서 회원탈퇴를할때,   해당회원이 글이나 코멘트를 쓴게있으면 먼저 삭제를다해줘야 삭제가 되는것같다.

 

사용자들의 글들이 서비스입장에서는 asset이니깐 , 이걸 지우면안되니깐 안지워지는방법을 찾고잇는중에

프리즈마에서는 해당 기능을 support  를하지않는다는 개소리가잇었다.

https://www.prisma.io/docs/guides/database/advanced-database-tasks/cascading-deletes/mysql

 

Configure cascading deletes with Prisma and MySQL

Learn how to configure cascading deletes with Prisma and MySQL by following the step-by-step instructions in this practical guide.

www.prisma.io

그래서 mySQL 에직접가서 SQL문을 돌려야한다고햇는데 너무하기싫었다.

너무하기싫어서 프리즈마 공식문서를 더 뒤져봤더니!!!!!!!!!!!!!1

2.26버젼이상은  지원을해준다고써있다.

현재 2.28버젼인데....... 공식문서를 첨부터  된다고 써야되지않겟니 게으른것들아

 

https://www.prisma.io/docs/concepts/components/prisma-schema/relations/referential-actions/#referential-action-defaults

 

Referential actions (Preview)

Referential actions let you define the update and delete behavior of related models on the database level

www.prisma.io

그래서 기분좋게  onDelete: SetNull 로 해결할수있다.

프리즈마 사랑해요. 

 

그럼 코멘트와 글 삭제하지않고 유저를 지우면,  유저가  null  로들어간다.

이쯤에서 드는 궁금증은 그럼 ,

삭제한사람의 글이나 코맨트 작성자는 프론트에서 어떻게 해결해줄까 그냥 null로 보이면안될것같은데....라고 생각햇지만 

우리한텐 짱짱동민님이있으니깐 해결해주시겠지.... 라고 하고 넘어갓다. 

더보기

이 기능을 할려면 ,prisma schema에서

previewFeatures에서 referentailAction을 enable 해주고

 

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["referentialActions"]
}
datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model User {
  nickname      String     @id
  email         String     @unique
  password      String

 
model Potato {
  profileId  Int     @id @default(autoincrement())
  profilePic String?
  role       String?

  user     User?   @relation(fields: [nickname], references: [nickname],onDelete: SetNull)
  nickname String?
}

 relation을 optional로 변경 (User? / String? 하고나서  onDelete:SetNull)로 변경가능!!!!!!!

 

반응형

댓글()