Skip to main content

排除字段

默认情况下,Prisma 客户端返回模型中的所有字段。你可以使用 select 来缩小结果集,但如果你有一个大型模型并且只想排除少数字段,这可能会很麻烦。

¥By default Prisma Client returns all fields from a model. You can use select to narrow the result set, but that can be unwieldy if you have a large model and you only want to exclude a small number of fields.

信息

从 Prisma ORM 6.2.0 开始,通过你可以传递给 Prisma Client 的 omit 选项支持排除字段。从版本 5.16.0 到 6.1.0,你必须使用 omitApi 预览功能才能访问此选项。

¥As of Prisma ORM 6.2.0, excluding fields is supported via the omit option that you can pass to Prisma Client. From versions 5.16.0 through 6.1.0, you must use the omitApi Preview feature to access this option.

使用 omit 全局排除字段

¥Excluding a field globally using omit

以下是全局排除字段的类型安全方法(即针对给定模型的所有查询):

¥The following is a type-safe way to exclude a field globally (i.e. for all queries against a given model):

const prisma = new PrismaClient({
omit: {
user: {
password: true
}
}
})

// The password field is excluded in all queries, including this one
const user = await prisma.user.findUnique({ where: { id: 1 } })

使用 omit 局部排除字段

¥Excluding a field locally using omit

以下是本地排除字段的类型安全方法(即针对单个查询):

¥The following is a type-safe way to exclude a field locally (i.e. for a single query):

const prisma = new PrismaClient()

// The password field is excluded only in this query
const user = await prisma.user.findUnique({
omit: {
password: true
},
where: {
id: 1
}
})

如何省略多个字段

¥How to omit multiple fields

省略多个字段与选择多个字段的工作方式相同:将多个键值对添加到 omit 选项。使用与以前相同的模式,你可以省略密码和电子邮件,如下所示:

¥Omitting multiple fields works the same as selecting multiple fields: add multiple key-value pairs to the omit option. Using the same schema as before, you could omit password and email with the following:

const prisma = new PrismaClient()

// password and email are excluded
const user = await prisma.user.findUnique({
omit: {
email: true,
password: true,
},
where: {
id: 1,
},
})

可以在本地和全局省略多个字段。

¥Multiple fields can be omitted locally and globally.

如何选择先前省略的字段

¥How to select a previously omitted field

如果你 全局省略字段,你可以通过专门选择字段或在查询中将 omit 设置为 false 来 "override"。

¥If you omit a field globally, you can "override" by either selecting the field specifically or by setting omit to false in a query.

const user = await prisma.user.findUnique({
select: {
firstName: true,
lastName: true,
password: true // The password field is now selected.
},
where: {
id: 1
}
})

何时全局或局部使用 omit

¥When to use omit globally or locally

了解何时全局或本地省略字段非常重要:

¥It's important to understand when to omit a field globally or locally:

  • 如果你省略字段是为了防止它意外包含在查询中,最好在全局省略它。例如:从 User 模型中全局省略 password 字段,以免敏感信息意外泄露。

    ¥If you are omitting a field in order to prevent it from accidentally being included in a query, it's best to omit it globally. For example: Globally omitting the password field from a User model so that sensitive information doesn't accidentally get exposed.

  • 如果你省略字段是因为查询中不需要它,最好在本地省略它。

    ¥If you are omitting a field because it's not needed in a query, it's best to omit it locally.

本地省略(当查询中提供 omit 选项时)仅适用于定义它的查询,而全局省略适用于使用相同 Prisma Client 实例 除非使用特定选择或忽略被覆盖 进行的每个查询。

¥Local omit (when an omit option is provided in a query) only applies to the query it is defined in, while a global omit applies to every query made with the same Prisma Client instance, unless a specific select is used or the omit is overridden.