Skip to main content

使用复合 ID 和唯一约束

可以使用 @@id@@unique 属性在 Prisma 架构中定义复合 ID 和复合唯一约束。

¥Composite IDs and compound unique constraints can be defined in your Prisma schema using the @@id and @@unique attributes.

warning

MongoDB 不支持 @@id
MongoDB 不支持复合 ID,这意味着你无法识别具有 @@id 属性的模型。

¥MongoDB does not support @@id
MongoDB does not support composite IDs, which means you cannot identify a model with a @@id attribute.

复合 ID 或复合唯一约束使用两个字段的组合值作为数据库表中的主键或标识符。在以下示例中,postId 字段和 userId 字段用作 Like 表的复合 ID:

¥A composite ID or compound unique constraint uses the combined values of two fields as a primary key or identifier in your database table. In the following example, the postId field and userId field are used as a composite ID for a Like table:

model User {
id Int @id @default(autoincrement())
name String
post Post[]
likes Like[]
}

model Post {
id Int @id @default(autoincrement())
content String
User User? @relation(fields: [userId], references: [id])
userId Int?
likes Like[]
}

model Like {
postId Int
userId Int
User User @relation(fields: [userId], references: [id])
Post Post @relation(fields: [postId], references: [id])

@@id([postId, userId])
}

Like 表中查询记录(例如使用 prisma.like.findMany())将返回如下所示的对象:

¥Querying for records from the Like table (e.g. using prisma.like.findMany()) would return objects that look as follows:

{
"postId": 1,
"userId": 1
}

尽管响应中只有两个字段,但这两个字段组成了一个名为 postId_userId 的复合 ID。

¥Although there are only two fields in the response, those two fields make up a compound ID named postId_userId.

你还可以使用 @@id@@unique 属性的 name 字段创建命名复合 ID 或复合唯一约束。例如:

¥You can also create a named compound ID or compound unique constraint by using the @@id or @@unique attributes' name field. For example:

model Like {
postId Int
userId Int
User User @relation(fields: [userId], references: [id])
Post Post @relation(fields: [postId], references: [id])

@@id(name: "likeId", [postId, userId])
}

你可以在哪里使用复合 ID 和唯一约束

¥Where you can use compound IDs and unique constraints

处理唯一数据时可以使用复合 ID 和复合唯一约束。

¥Compound IDs and compound unique constraints can be used when working with unique data.

以下是在查询的 where 过滤器中接受复合 ID 或复合唯一约束的 Prisma 客户端函数列表:

¥Below is a list of Prisma Client functions that accept a compound ID or compound unique constraint in the where filter of the query:

  • findUnique()

  • findUniqueOrThrow

  • delete

  • update

  • upsert

当使用 connectconnectOrCreate 创建关系数据时,也可以使用复合 ID 和复合唯一约束。

¥A composite ID and a composite unique constraint is also usable when creating relational data with connect and connectOrCreate.

按复合 ID 或唯一约束过滤记录

¥Filtering records by a compound ID or unique constraint

尽管你的查询结果不会将复合 ID 或唯一约束显示为字段,但你可以使用这些复合值来过滤查询以获取唯一记录:

¥Although your query results will not display a compound ID or unique constraint as a field, you can use these compound values to filter your queries for unique records:

const like = await prisma.like.findUnique({
where: {
likeId: {
userId: 1,
postId: 1,
},
},
})
info

注意 复合 ID 和复合唯一约束键仅可用作唯一查询(例如 findUnique()findUniqueOrThrow)的筛选选项。请参阅上面的 section,了解可能使用这些字段的位置列表。

¥Note composite ID and compound unique constraint keys are only available as filter options for unique queries such as findUnique() and findUniqueOrThrow. See the section above for a list of places these fields may be used.

通过复合 ID 或唯一约束删除记录

¥Deleting records by a compound ID or unique constraint

复合 ID 或复合唯一约束可以用在 delete 查询的 where 过滤器中:

¥A compound ID or compound unique constraint may be used in the where filter of a delete query:

const like = await prisma.like.delete({
where: {
likeId: {
userId: 1,
postId: 1,
},
},
})

通过复合 ID 或唯一约束更新和更新插入记录

¥Updating and upserting records by a compound ID or unique constraint

复合 ID 或复合唯一约束可以用在 update 查询的 where 过滤器中:

¥A compound ID or compound unique constraint may be used in the where filter of an update query:

const like = await prisma.like.update({
where: {
likeId: {
userId: 1,
postId: 1,
},
},
data: {
postId: 2,
},
})

它们也可以用在 upsert 查询的 where 过滤器中:

¥They may also be used in the where filter of an upsert query:

await prisma.like.upsert({
where: {
likeId: {
userId: 1,
postId: 1,
},
},
update: {
userId: 2,
},
create: {
userId: 2,
postId: 1,
},
})

通过复合 ID 或唯一约束过滤关系查询

¥Filtering relation queries by a compound ID or unique constraint

连接记录以创建关系时使用的 connectconnectOrCreate 键中也可以使用复合 ID 和复合唯一约束。

¥Compound IDs and compound unique constraint can also be used in the connect and connectOrCreate keys used when connecting records to create a relationship.

例如,考虑以下查询:

¥For example, consider this query:

await prisma.user.create({
data: {
name: 'Alice',
likes: {
connect: {
likeId: {
postId: 1,
userId: 2,
},
},
},
},
})

likeId 复合 ID 用作 connect 对象中的标识符,该对象用于定位将链接到新用户的 Like 表记录:"Alice"

¥The likeId compound ID is used as the identifier in the connect object that is used to locate the Like table's record that will be linked to the new user: "Alice".

同样,likeId 可以在 connectOrCreatewhere 过滤器中使用,以尝试定位 Like 表中的现有记录:

¥Similarly, the likeId can be used in connectOrCreate's where filter to attempt to locate an existing record in the Like table:

await prisma.user.create({
data: {
name: 'Alice',
likes: {
connectOrCreate: {
create: {
postId: 1,
},
where: {
likeId: {
postId: 1,
userId: 1,
},
},
},
},
},
})