在 JavaScript 和 CockroachDB 中使用 Prisma Migrate
建立資料庫模式
在本指南中,您將使用 Prisma Migrate 在資料庫中建立表。將以下 Prisma 資料模型新增到您的 Prisma 模式檔案 prisma/schema.prisma 中
prisma/schema.prisma
model Post {
id BigInt @id @default(sequence())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId BigInt
}
model Profile {
id BigInt @id @default(sequence())
bio String?
user User @relation(fields: [userId], references: [id])
userId BigInt @unique
}
model User {
id BigInt @id @default(sequence())
email String @unique
name String?
posts Post[]
profile Profile?
}
要將資料模型對映到資料庫模式,您需要使用 prisma migrate CLI 命令
npx prisma migrate dev --name init
此命令執行兩項操作
- 它為本次遷移建立一個新的 SQL 遷移檔案
- 它針對資料庫執行 SQL 遷移檔案
注意
預設情況下,在執行 prisma migrate dev 後,會自動呼叫 generate。如果您的模式中定義了 prisma-client-js 生成器,它將檢查 @prisma/client 是否已安裝,如果缺失則會安裝。
太棒了,您現在已經使用 Prisma Migrate 在資料庫中建立了三張表 🚀