Skip to main content

使用标量列表

标量列表[] 修饰符表示,并且仅在底层数据库支持标量列表时才可用。以下示例有一个名为 pets 的标量 String 列表:

¥Scalar lists are represented by the [] modifier and are only available if the underlying database supports scalar lists. The following example has one scalar String list named pets:

model User {
id Int @id @default(autoincrement())
name String
pets String[]
}

字段值示例:

¥Example field value:

['Fido', 'Snoopy', 'Brian']

设置标量列表的值

¥Setting the value of a scalar list

以下示例演示了在创建模型时如何对标量列表 (coinflips) 的值进行 set

¥The following example demonstrates how to set the value of a scalar list (coinflips) when you create a model:

const createdUser = await prisma.user.create({
data: {
email: 'eloise@prisma.io',
coinflips: [true, true, true, false, true],
},
})

取消设置标量列表的值

¥Unsetting the value of a scalar list

warning

该方法仅适用于 MongoDB 3.11.1 及更高版本。

¥This method is available on MongoDB only in versions 3.11.1 and later.

以下示例演示如何对标量列表的值 (coinflips) 进行 unset

¥The following example demonstrates how to unset the value of a scalar list (coinflips):

const createdUser = await prisma.user.create({
data: {
email: 'eloise@prisma.io',
coinflips: {
unset: true,
},
},
})

set: null 不同,unset 完全删除了该列表。

¥Unlike set: null, unset removes the list entirely.

将项目添加到标量列表

¥Adding items to a scalar list

warning

可以用来:

¥Available for:

  • 2.15.0 及更高版本的 PostgreSQL

    ¥PostgreSQL in versions 2.15.0 and later

  • 3.9.0 及更高版本中的 CockroachDB

    ¥CockroachDB in versions 3.9.0 and later

  • 版本 3.11.0 及更高版本的 MongoDB

    ¥MongoDB in versions 3.11.0 and later

使用 push 方法将单个值添加到标量列表:

¥Use the push method to add a single value to a scalar list:

const userUpdate = await prisma.user.update({
where: {
id: 9,
},
data: {
coinflips: {
push: true,
},
},
})

在早期版本中,你必须覆盖整个值。以下示例检索用户,使用 push() 添加三个新的硬币翻转,并覆盖 update 中的 coinflips 字段:

¥In earlier versions, you have to overwrite the entire value. The following example retrieves user, uses push() to add three new coin flips, and overwrites the coinflips field in an update:

const user = await prisma.user.findUnique({
where: {
email: 'eloise@prisma.io',
},
})

if (user) {
console.log(user.coinflips)

user.coinflips.push(true, true, false)

const updatedUser = await prisma.user.update({
where: {
email: 'eloise@prisma.io',
},
data: {
coinflips: user.coinflips,
},
})

console.log(updatedUser.coinflips)
}

过滤标量列表

¥Filtering scalar lists

warning

可以用来:

¥Available for:

  • 2.15.0 及更高版本的 PostgreSQL

    ¥PostgreSQL in versions 2.15.0 and later

  • 3.9.0 及更高版本中的 CockroachDB

    ¥CockroachDB in versions 3.9.0 and later

  • 版本 3.11.0 及更高版本的 MongoDB

    ¥MongoDB in versions 3.11.0 and later

使用 标量列表过滤器 筛选具有与特定条件匹配的标量列表的记录。以下示例返回标签列表包含 databasestypescript 的所有帖子:

¥Use scalar list filters to filter for records with scalar lists that match a specific condition. The following example returns all posts where the tags list includes databases and typescript:

const posts = await prisma.post.findMany({
where: {
tags: {
hasEvery: ['databases', 'typescript'],
},
},
})

数组中的 NULL

¥NULL values in arrays

warning

本节适用于:

¥This section applies to:

  • 2.15.0 及更高版本的 PostgreSQL

    ¥PostgreSQL in versions 2.15.0 and later

  • 3.9.0 及更高版本中的 CockroachDB

    ¥CockroachDB in versions 3.9.0 and later

将标量列表过滤器与关系数据库连接器一起使用时,以下条件不考虑具有 NULL 值的数组字段:

¥When using scalar list filters with a relational database connector, array fields with a NULL value are not considered by the following conditions:

  • NOT(数组不包含 X)

    ¥NOT (array does not contain X)

  • isEmpty(数组为空)

    ¥isEmpty (array is empty)

这意味着你可能期望看到的记录不会返回。考虑以下示例:

¥This means that records you might expect to see are not returned. Consider the following examples:

  • 以下查询返回 tags 不包含 databases 的所有帖子:

    ¥The following query returns all posts where the tags do not include databases:

    const posts = await prisma.post.findMany({
    where: {
    NOT: {
    tags: {
    has: 'databases',
    },
    },
    },
    })
    • ✔ 不包含 "databases" 的数组,例如 {"typescript", "graphql"}

      ¥✔ Arrays that do not contain "databases", such as {"typescript", "graphql"}

    • ✔ 空数组,例如 []

      ¥✔ Empty arrays, such as []

    查询不返回:

    ¥The query does not return:

    • NULL 数组,即使它们不包含 "databases"

      ¥✘ NULL arrays, even though they do not contain "databases"

以下查询返回 tags 为空的所有帖子:

¥The following query returns all posts where tags is empty:

const posts = await prisma.post.findMany({
where: {
tags: {
isEmpty: true,
},
},
})

查询返回:

¥The query returns:

  • ✔ 空数组,例如 []

    ¥✔ Empty arrays, such as []

查询不返回:

¥The query does not return:

  • NULL 数组,即使它们可以被视为空

    ¥✘ NULL arrays, even though they could be considered empty

要解决此问题,你可以将数组字段的默认值设置为 []

¥To work around this issue, you can set the default value of array fields to [].