在一个查询中更新多个表
Update multiple tables in one query
我找不到有关 Prisma 文档的任何相关信息,也找不到关于此问题的相关信息。
我的相关架构如下所示:
model User {
id Int @default(autoincrement()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
firstName String?
lastName String?
email String @unique
hashedPassword String?
role String @default("user")
sessions Session[]
profile Profile?
}
model Profile {
id Int @default(autoincrement()) @id
aboutMe String?
location String?
profession String?
user User @relation(fields:[userId], references: [id])
userId Int
}
我想更新两个表中的多个列,但是无法在 Prisma 中进行更改。所以,这就是我的变异目前的样子:
...
const {aboutMe, location, profession, firstName, lastName } = inputs;
const profile = await db.user.update({
where: { id: ctx.session!.userId },
data: {
profile: {
update: {
aboutMe,
location,
profession,
},
},
},
});
const user = await db.user.update({
where: { id: ctx.session!.userId },
data: {
firstName,
lastName,
},
});
...
如您所见,我有两个突变,是否可以用一个突变更新多个表?
谢谢。
这应该有效:
const profile = await db.user.update({
where: { id: u.id },
data: {
firstName: 'name',
lastName: 'name',
profile: {
update: {
aboutMe: 'aboutMe',
location: 'aboutMe',
profession: 'aboutMe',
},
},
},
include: { profile: true },
})
我找不到有关 Prisma 文档的任何相关信息,也找不到关于此问题的相关信息。
我的相关架构如下所示:
model User {
id Int @default(autoincrement()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
firstName String?
lastName String?
email String @unique
hashedPassword String?
role String @default("user")
sessions Session[]
profile Profile?
}
model Profile {
id Int @default(autoincrement()) @id
aboutMe String?
location String?
profession String?
user User @relation(fields:[userId], references: [id])
userId Int
}
我想更新两个表中的多个列,但是无法在 Prisma 中进行更改。所以,这就是我的变异目前的样子:
...
const {aboutMe, location, profession, firstName, lastName } = inputs;
const profile = await db.user.update({
where: { id: ctx.session!.userId },
data: {
profile: {
update: {
aboutMe,
location,
profession,
},
},
},
});
const user = await db.user.update({
where: { id: ctx.session!.userId },
data: {
firstName,
lastName,
},
});
...
如您所见,我有两个突变,是否可以用一个突变更新多个表?
谢谢。
这应该有效:
const profile = await db.user.update({
where: { id: u.id },
data: {
firstName: 'name',
lastName: 'name',
profile: {
update: {
aboutMe: 'aboutMe',
location: 'aboutMe',
profession: 'aboutMe',
},
},
},
include: { profile: true },
})