跳到主要內容

將 Prisma Migrate 與 TypeScript 和 PlanetScale 結合使用

建立資料庫 schema

本指南中,您將使用 Prisma 的 db push 命令來在資料庫中建立表。將以下 Prisma 資料模型新增到 prisma/schema.prisma 中的 Prisma schema:

prisma/schema.prisma
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int

@@index(authorId)
}

model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique

@@index(userId)
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
}

現在您已準備好將新 schema 推送到資料庫。按照連線您的資料庫中的說明連線到您的 main 分支。

現在使用 db push CLI 命令推送到 main 分支

npx prisma db push

太棒了,您現在使用 Prisma 的 db push 命令在資料庫中建立了三張表 🚀

© . This site is unofficial and not affiliated with Prisma Data, Inc.