Skip to main content

json 协议变更

从 Prisma ORM 版本 5.0.0 开始,新的 jsonProtocol 是默认值。有一些更改是此更改直接导致的,还有一些更改与新协议相关。

¥As of Prisma ORM version 5.0.0, the new jsonProtocol is the default. There are some changes that directly result from this change and a few changes that are related to the new protocol.

Prisma ORM 5 更改的完整列表可以在 在我们的发行说明中 中找到。

¥A full list of Prisma ORM 5 changes can be found in our release notes.

jsonProtocol 具体变化

¥jsonProtocol specific changes

以下是 jsonProtocol 功能成为 Prisma ORM 5 中默认功能直接导致的更改。

¥Below are changes that result directly from the jsonProtocol feature becoming the default in Prisma ORM 5.

删除 jsonProtocol 预览功能

¥Removal of jsonProtocol Preview Feature

在 Prisma ORM 5 中,jsonProtocol 是 Prisma Client 中默认且唯一的协议。不再需要 jsonProtocol 预览功能。

¥In Prisma ORM 5, jsonProtocol is the default and only protocol in Prisma Client. The jsonProtocol Preview feature is no longer needed.

Prisma ORM 4 及更低版本:

¥Prisma ORM 4 and lower:

generator client {
provider = "prisma-client-js"
previewFeatures = ["jsonProtocol"]
}

Prisma ORM 5:

generator client {
provider = "prisma-client-js"
}

改进的错误消息

¥Improved error messages

由于切换到新协议,一些错误消息已得到改进。例如,Prisma ORM 4 及以下版本中出现以下错误消息:

¥Due to the switch to the new protocol, several error messages have been improved. For example, the following error message in Prisma ORM 4 and below:

Failed to validate the query: `Unable to match input value to any allowed input type for the field. Parse errors: [Query parsing/validation error at `Mutation.createOneUser.data.UserCreateInput.person.PersonCreateNestedOneWithoutUserInput.create`: Unable to match input value to any allowed input type for the field. Parse errors: [Query parsing/validation error at `Mutation.createOneUser.data.UserCreateInput.person.PersonCreateNestedOneWithoutUserInput.create.PersonCreateWithoutUserInput.hubspot_id`: A value is required but not set., Query parsing/validation error at `Mutation.createOneUser.data.UserCreateInput.person.PersonCreateNestedOneWithoutUserInput.create.PersonUncheckedCreateWithoutUserInput.hubspot_id`: A value is required but not set.], Query parsing/validation error at `Mutation.createOneUser.data.UserUncheckedCreateInput.person`: Field does not exist on enclosing type.]` at `Mutation.createOneUser.data`

在 Prisma ORM 5 中变为以下内容:

¥becomes the following in Prisma ORM 5:

Invalid `prisma.user.create()` invocation in
/Users/prismo/projects/prisma/reproductions/workbench/index.ts:21:36

18 const prisma = new PrismaClient()
19
20 for (const u of userData) {
→ 21 const user = await prisma.user.create({
data: {
email: "eugene.albright@gallaudet.edu",
person: {
create: {
first_name: "William",
last_name: "Albright",
+ hubspot_id: String
}
}
}
})

Argument `hubspot_id` must not be null.

jsonProtocol 相关变更

¥jsonProtocol related changes

以下是与切换到新协议相关的更改。如果你使用的是 jsonProtocol 预览功能,你很可能会遇到这些问题。

¥Below are changes that are related to the switch to the new protocol. If you were using the jsonProtocol Preview Feature, you most likely ran into these issues.

删除数组快捷方式

¥Removal of array shortcuts

作为此重大更新的一部分,删除了多个数组快捷方式。这些快捷方式是将单个元素作为值添加到基于数组的运算符的一种方法。

¥Several array shortcuts were removed as a part of this major update. These shortcuts were a way to add a single element as a value to an array-based operator.

OR 运算符

¥OR operators

Prisma ORM 4 及更低版本中的以下代码:

¥The following code in Prisma ORM 4 and lower:

prisma.user.findMany({
where: {
OR: { email: 'foo@example.com' },
},
})

在 Prisma ORM 5 中需要更改为以下内容:

¥Will need to be changed to the following in Prisma ORM 5:

prisma.user.findMany({
where: {
OR: [{ email: 'foo@example.com' }],
},
})

OR 运算符仅接受数组值。

¥OR operators will only accept array values.

innotIn 运算符

¥in and notIn operators

OR 类似,innotIn 需要数组值。

¥Similar to OR, in and notIn require array values.

Prisma ORM 4 及更低版本:

¥Prisma ORM 4 and lower:

prisma.user.findMany({
where: {
id: { in: 123 },
},
})

prisma.user.findMany({
where: {
id: { notIn: 123 },
},
})

Prisma ORM 5:

prisma.user.findMany({
where: {
id: {
in: [123],
},
},
})

prisma.user.findMany({
where: {
id: {
notIn: [123],
},
},
})
Suggestion for single elements

如果你的 innotIn 值只是一个元素,你还可以更新代码以根本不使用这些运算符:

¥If your in and notIn values are only one element, you can also update your code to not use these operators at all:

prisma.user.findMany({
where: {
id: 123,
},
})

prisma.user.findMany({
where: {
id: { not: 123 },
},
})

用于过滤 PostgreSQL 中 JSON 字段的 path 参数

¥path argument for filtering on JSON fields in PostgreSQL

在 PostgreSQL 模型中过滤 JSON 字段时path 参数现在只接受数组。

¥When filtering on JSON fields in a PostgreSQL model the path argument now only accepts an array.

当使用以下架构时:

¥When using the following schema:

model User {
id String @id
settings Json
}

Prisma ORM 4 及更低版本:

¥Prisma ORM 4 and lower:

prisma.user.findMany({
where: {
settings: {
path: 'someSetting',
equals: someValue,
},
},
})

Prisma ORM 5:

prisma.user.findMany({
where: {
settings: {
path: ['someSetting'],
equals: someValue,
},
},
})
info

注意:此 path 参数更改仅影响 PostgreSQL 数据库。MySQL 数据库不受影响,因为它们使用不同的语法。

¥Note: This path argument change only affects PostgreSQL databases. MySQL databases are not affected as they use a different syntax.

标量列表

¥Scalar lists

在所有操作中,标量列表 值必须是数组。

¥Scalar list values must be arrays in all operations.

具有以下架构:

¥With the following schema:

model Post {
id String @id @default(uuid())
tags String[]
}

Prisma ORM 4 及更低版本:

¥Prisma ORM 4 and lower:

prisma.post.create({
data: {
tags: 'databases',
},
})

prisma.post.findMany({
where: {
tags: 'databases',
},
})

Prisma ORM 5:

prisma.post.create({
data: {
tags: ['databases'],
},
})

prisma.post.findMany({
where: {
tags: ['databases'],
},
})

复合列表

¥Composite lists

复合类型(对于 MongoDB)列表的操作现在仅接受数组值。

¥Operations on lists of Composite types (for MongoDB) now only accept array values.

具有以下架构:

¥With the following schema:

model Post {
id String @id @default(uuid())
commentsList Comment[]
}

type Comment {
text String
}

Prisma ORM 4 及更低版本:

¥Prisma ORM 4 and lower:

prisma.post.findMany({
where: {
commentsList: {
equals: { text: 'hello' },
},
},
})

Prisma ORM 5:

prisma.post.findMany({
where: {
commentsList: {
equals: [{ text: 'hello' }],
},
},
})
Shorthand notation usage

如果你使用简写符号并排除 equals,你仍然必须为复合列表字段提供数组值。

¥If you use the shorthand notation and exclude equals, you still must supply an array value for composite list fields.

Prisma 4 及更低版本:

¥Prisma 4 and lower:

prisma.post.create({
data: {
commentsList: { text: 'hello' },
},
})

prisma.post.findMany({
where: {
commentsList: { text: 'hello' },
},
})

Prisma 5:

prisma.post.create({
data: {
commentsList: [{ text: 'hello' }],
},
})

prisma.post.findMany({
where: {
commentsList: [{ text: 'hello' }],
},
})