<!-- 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 }
model Profile { id Int@id@default(autoincrement()) bio String? user User @relation(fields: [userId], references: [id]) userId Int@unique }
model User { id Int@id@default(autoincrement()) email String @unique name String? posts Post[] profile Profile? }
执行以下命令会创建一份 sql 查询语句文件并且执行,创建三张表格
npx prisma migrate dev --name init
2、数据库读写
(1) 数据库查询
创建 index.ts 文件,写入一下内容
import { PrismaClient } from'@prisma/client'
const prisma = new PrismaClient()
asyncfunctionmain() { // ... you will write your Prisma Client queries here const allUsers = await prisma.user.findMany() console.log(allUsers) }