我試圖為房間管理系統建立一個資料庫,但我對這些關係感到困惑,並且在網路上找不到有用的資源,你能告訴我這個 prisma 腳本是否有問題嗎?因為我想在expressJs中控制它並基於它製作一個應用程式
generator client { provider = "prisma-client-js" } datasource db { provider = "mysql" url = env("DATABASE_URL") } model Guest { guestId String @id @default(uuid()) name String phone String @unique() address String? nationality String Reservation Reservation[] @relation("GuestReservation") } model Reservation { reservationId Int @id @default(autoincrement()) checkIn DateTime checkOut DateTime Guest Guest @relation("GuestReservation", fields: [guestId], references: [guestId], onDelete: Cascade) guestId String visitors Int Room Room @relation("RoomReservation", fields: [roomId], references: [roomId], onDelete: Cascade) type ReservationType roomId Int Bill Bill? @relation("BillReservation") } enum ReservationType { Booking Contract Booked Canceled } model Room { roomId Int @id @default(autoincrement()) price Float type Type Reservation Reservation[] @relation("RoomReservation") } enum Type { Single Double Triple } model Bill { invoiceNo String @id @default(uuid()) Reservation Reservation @relation("BillReservation", fields: [reservationId], references: [reservationId], onDelete: Cascade) reservationId Int @unique() roomService Float @default(0) paymentMode Payment Service Service[] } enum Payment { Cash Visa } model Service { serviceId String @id @default(uuid()) type ServiceType name String price Float Bill Bill @relation(fields: [billInvoiceNo], references: [invoiceNo], onDelete: Cascade) billInvoiceNo String } enum ServiceType { Bar Laundry }
我嘗試為每個實體創建一個粗略的內容,但最終出現了關係錯誤,例如外來密鑰之類的東西,這意味著我的關係有問題。
您的架構是有效的,但我建議在為表定義 id 時保持一致性。有些表的id是字串類型,有些表的id是數字類型。
以下是為模型建立實體的查詢範例。
這是查詢回應: