自省
使用 Prisma ORM 对你的数据库进行自省
在本指南中,我们将使用一个包含三个表的示例 SQL schema
CREATE TABLE "User" (
id INT8 PRIMARY KEY DEFAULT unique_rowid(),
name STRING(255),
email STRING(255) UNIQUE NOT NULL
);
CREATE TABLE "Post" (
id INT8 PRIMARY KEY DEFAULT unique_rowid(),
title STRING(255) UNIQUE NOT NULL,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
content STRING,
published BOOLEAN NOT NULL DEFAULT false,
"authorId" INT8 NOT NULL,
FOREIGN KEY ("authorId") REFERENCES "User"(id)
);
CREATE TABLE "Profile" (
id INT8 PRIMARY KEY DEFAULT unique_rowid(),
bio STRING,
"userId" INT8 UNIQUE NOT NULL,
FOREIGN KEY ("userId") REFERENCES "User"(id)
);
注意:某些字段使用双引号,以确保 CockroachDB 使用正确的字母大小写。如果未使用双引号,CockroachDB 将把所有内容都读取为小写字符。
展开查看表的图形概览
用户
列名 | 类型 | 主键 | 外键 | 必需 | 默认值 |
---|---|---|---|---|---|
id | INT8 | ✔️ | 否 | ✔️ | 自增 |
name | STRING(255) | 否 | 否 | 否 | - |
email | STRING(255) | 否 | 否 | ✔️ | - |
Post
列名 | 类型 | 主键 | 外键 | 必需 | 默认值 |
---|---|---|---|---|---|
id | INT8 | ✔️ | 否 | ✔️ | 自增 |
createdAt | TIMESTAMP | 否 | 否 | ✔️ | now() |
title | STRING(255) | 否 | 否 | ✔️ | - |
content | STRING | 否 | 否 | 否 | - |
published | BOOLEAN | 否 | 否 | ✔️ | false |
authorId | INT8 | 否 | ✔️ | ✔️ | - |
Profile
列名 | 类型 | 主键 | 外键 | 必需 | 默认值 |
---|---|---|---|---|---|
id | INT8 | ✔️ | 否 | ✔️ | 自增 |
bio | STRING | 否 | 否 | 否 | - |
userId | INT8 | 否 | ✔️ | ✔️ | - |
下一步,你将对数据库进行自省。自省的结果将是在你的 Prisma schema 中的一个数据模型。
运行以下命令对你的数据库进行自省
npx prisma db pull
此命令读取用于在你的 schema.prisma
中定义 url
的环境变量 DATABASE_URL
,在我们的例子中,此变量设置在 .env
文件中,用于连接到你的数据库。一旦建立连接,它就会对数据库进行自省(即,读取数据库 schema)。然后,它将数据库 schema 从 SQL 转换为 Prisma 数据模型。
自省完成后,你的 Prisma schema 会被更新
数据模型现在看起来类似于这样
model Post {
id BigInt @id @default(autoincrement())
title String @unique @db.String(255)
createdAt DateTime @default(now()) @db.Timestamp(6)
content String?
published Boolean @default(false)
authorId BigInt
User User @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model Profile {
id BigInt @id @default(autoincrement())
bio String?
userId BigInt @unique
User User @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model User {
id BigInt @id @default(autoincrement())
name String? @db.String(255)
email String @unique @db.String(255)
Post Post[]
Profile Profile?
}
Prisma ORM 的数据模型是你的数据库 schema 的声明性表示,并作为生成的 Prisma Client 库的基础。你的 Prisma Client 实例将暴露针对这些模型定制的查询。
目前,数据模型有一些小“问题”
User
关系字段是大写的,因此不符合 Prisma 的命名约定。为了表达更多的“语义”,如果此字段命名为author
以更好地描述User
和Post
之间的关系,也会更好。User
上的Post
和Profile
关系字段,以及Profile
上的User
关系字段,都是大写的。为了遵守 Prisma 的命名约定,这两个字段都应该小写为post
、profile
和user
。- 即使小写后,
User
上的post
字段仍然略有误命名。这是因为它实际上是指一个列表的帖子——因此,更好的名称应该是复数形式:posts
。
这些更改与生成的 Prisma Client API 相关,在其中使用小写关系字段 author
、posts
、profile
和 user
对 JavaScript/TypeScript 开发者来说会感觉更自然和符合习惯。因此,你可以配置你的 Prisma Client API。
由于关系字段是虚拟的(即它们不会直接在数据库中体现),你可以在不触碰数据库的情况下,在你的 Prisma schema 中手动重命名它们
model Post {
id BigInt @id @default(autoincrement())
title String @unique @db.String(255)
createdAt DateTime @default(now()) @db.Timestamp(6)
content String?
published Boolean @default(false)
authorId BigInt
author User @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model Profile {
id BigInt @id @default(autoincrement())
bio String?
userId BigInt @unique
user User @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}
model User {
id BigInt @id @default(autoincrement())
name String? @db.String(255)
email String @unique @db.String(255)
posts Post[]
profile Profile?
}
在这个例子中,数据库 schema 遵循了 Prisma ORM 模型的命名约定(只有通过自省生成的虚拟关系字段不符合这些约定,需要调整)。这优化了生成的 Prisma Client API 的易用性。
使用自定义模型和字段名称
然而,有时你可能希望对 Prisma Client API 中公开的列和表的名称进行额外更改。一个常见的例子是将数据库 schema 中常用的 snake_case 命名法转换为 PascalCase 和 camelCase 命名法,这对于 JavaScript/TypeScript 开发者来说更自然。
假设你从基于 snake_case 命名法的自省中获得了以下模型
model my_user {
user_id Int @id @default(sequence())
first_name String?
last_name String @unique
}
如果你为这个模型生成 Prisma Client API,它会在 API 中沿用 snake_case 命名法
const user = await prisma.my_user.create({
data: {
first_name: 'Alice',
last_name: 'Smith',
},
})
如果你不想在 Prisma Client API 中使用数据库中的表和列名称,你可以使用@map
和 @@map
来配置它们
model MyUser {
userId Int @id @default(sequence()) @map("user_id")
firstName String? @map("first_name")
lastName String @unique @map("last_name")
@@map("my_user")
}
通过这种方法,你可以随意命名你的模型及其字段,并使用 @map
(用于字段名)和 @@map
(用于模型名)来指向底层表和列。你的 Prisma Client API 现在看起来像这样
const user = await prisma.myUser.create({
data: {
firstName: 'Alice',
lastName: 'Smith',
},
})
在配置你的 Prisma Client API 页面了解更多信息。