Prisma/MySQL不允许我删除三重相关记录

tf7tbtn2  于 2022-12-03  发布在  Mysql
关注(0)|答案(1)|浏览(157)

我有“用户”、“餐厅”、“评论”、“评论图像”表。当用户对带有图片的餐厅撰写评论时,该评论将保存为评论记录,其图片将保存在评论图像记录中。
我面临的问题是,在删除相关的ReviewImages记录之前,我无法删除Review记录。

model User {
  id                 String               @id @default(cuid())
  name               String
  nickname           String?              @unique
  bobId              String?              @unique
  email              String               @unique
  emailVerified      DateTime?
  image              String?              @db.VarChar(500)
  title              String?
  description        String?              @db.VarChar(500)
  accounts           Account[]
  sessions           Session[]
  VisitedRestaurants VisitedRestaurants[]
  Reviews            Reviews[]
  ReviewImages       ReviewImages[]
  ReviewLikes        ReviewLikes[]
  ReviewComments     ReviewComments[]
}

model Restaurants {
  id        String               @id @db.VarChar(50)
  poi_nm    String               @db.VarChar(100)
  branch_nm String?              @db.VarChar(100)
  sub_nm    String?              @db.VarChar(100)
  mcate_cd  String?              @db.VarChar(30)
  mcate_nm  String?              @db.VarChar(200)
  pnu       String?              @db.VarChar(30)
  sido_nm   String?              @db.VarChar(100)
  sgg_nm    String?              @db.VarChar(100)
  bemd_nm   String?              @db.VarChar(100)
  ri_nm     String?              @db.VarChar(100)
  beonji    String?              @db.VarChar(20)
  badm_cd   String?              @db.VarChar(30)
  hadm_cd   String?              @db.VarChar(30)
  rd_cd     String?              @db.VarChar(30)
  rd_nm     String?              @db.VarChar(100)
  bld_num   String?              @db.VarChar(30)
  grid_cd   String?              @db.VarChar(30)
  origin    String?              @db.VarChar(200)
  base_ymd  String?              @db.VarChar(20)
  createdAt DateTime             @default(now())
  loc       Unsupported("point")

  Reviews            Reviews[]
  VisitedRestaurants VisitedRestaurants[]
  ReviewImages       ReviewImages[]

  @@index(loc, map: "loc")
}

model Reviews {
  id           String      @unique @default(cuid())
  user         User        @relation(fields: [userId], references: [id])
  userId       String      @db.VarChar(255)
  restaurant   Restaurants @relation(fields: [restaurantId], references: [id])
  restaurantId String      @db.VarChar(50)
  title        String      @db.VarChar(300)
  titleLink    String      @unique @db.VarChar(500)
  content      String      @db.Text
  preview      String      @db.VarChar(255)
  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt

  ReviewImages   ReviewImages[]
  ReviewLikes    ReviewLikes[]
  ReviewComments ReviewComments[]

  @@id([userId, restaurantId])
}

model ReviewImages {
  id           String      @id @default(cuid())
  urls         String      @db.Text
  user         User        @relation(fields: [userId], references: [id])
  userId       String
  review       Reviews     @relation(fields: [reviewId], references: [id], onDelete: Cascade)
  reviewId     String
  restaurant   Restaurants @relation(fields: [restaurantId], references: [id])
  restaurantId String
  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt
}

在开发时,我使用Prisma Studio删除/添加记录,错误如下。

Type: undefined
Message: 
Invalid `prisma.reviews.delete()` invocation:

Foreign key constraint failed on the field: `reviewId`

Code: P2003

Query:
[object Object]

我猜ReviewImages模型在它的关系中有一些问题,但我不确定。

qqrboqgw

qqrboqgw1#

考虑以下简明模式文件:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["fieldReference"]
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model User {
  id            String         @id @default(cuid())
  name          String
  nickname      String?        @unique
  bobId         String?        @unique
  email         String         @unique
  emailVerified DateTime?
  image         String?        @db.VarChar(500)
  title         String?
  description   String?        @db.VarChar(500)
  Reviews       Reviews[]
  ReviewImages  ReviewImages[]
}

model Restaurants {
  id        String   @id @db.VarChar(50)
  poi_nm    String   @db.VarChar(100)
  branch_nm String?  @db.VarChar(100)
  sub_nm    String?  @db.VarChar(100)
  mcate_cd  String?  @db.VarChar(30)
  mcate_nm  String?  @db.VarChar(200)
  pnu       String?  @db.VarChar(30)
  sido_nm   String?  @db.VarChar(100)
  sgg_nm    String?  @db.VarChar(100)
  bemd_nm   String?  @db.VarChar(100)
  ri_nm     String?  @db.VarChar(100)
  beonji    String?  @db.VarChar(20)
  badm_cd   String?  @db.VarChar(30)
  hadm_cd   String?  @db.VarChar(30)
  rd_cd     String?  @db.VarChar(30)
  rd_nm     String?  @db.VarChar(100)
  bld_num   String?  @db.VarChar(30)
  grid_cd   String?  @db.VarChar(30)
  origin    String?  @db.VarChar(200)
  base_ymd  String?  @db.VarChar(20)
  createdAt DateTime @default(now())
  // loc       Unsupported("point")

  Reviews      Reviews[]
  ReviewImages ReviewImages[]

  // @@index(loc, map: "loc")
}

model Reviews {
  id           String      @unique @default(cuid())
  user         User        @relation(fields: [userId], references: [id])
  userId       String      @db.VarChar(255)
  restaurant   Restaurants @relation(fields: [restaurantId], references: [id])
  restaurantId String      @db.VarChar(50)
  title        String      @db.VarChar(300)
  titleLink    String      @unique @db.VarChar(500)
  content      String      @db.Text
  preview      String      @db.VarChar(255)
  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt

  ReviewImages ReviewImages[]

  @@id([userId, restaurantId])
}

model ReviewImages {
  id           String      @id @default(cuid())
  urls         String      @db.Text
  user         User        @relation(fields: [userId], references: [id])
  userId       String
  review       Reviews     @relation(fields: [reviewId], references: [id], onDelete: Cascade)
  reviewId     String
  restaurant   Restaurants @relation(fields: [restaurantId], references: [id])
  restaurantId String
  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt
}

这个剧本

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({
  log: ['query'],
});

async function main() {
  await prisma.user.deleteMany();

  await prisma.user.create({
    data: {
      email: 'test@prisma.io',
      name: 'Test',
      id: '1',
    },
  });

  await prisma.restaurants.create({
    data: {
      id: '1',
      poi_nm: 'test',
    },
  });

  await prisma.reviews.create({
    data: {
      title: 'test',
      content: 'test',
      preview: 'test',
      titleLink: 'test',
      restaurantId: '1',
      userId: '1',
      ReviewImages: {
        create: {
          urls: 'test',
          userId: '1',
          restaurantId: '1',
        },
      },
    },
  });
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

我能够删除审查记录,ReviewImage确实通过Prisma Studio删除。
我会建议您更新到最新的Prisma版本,以防您仍然面临这个问题。

相关问题