【发布时间】:2021-11-18 00:17:08
【问题描述】:
我正在使用 prisma + express + javascript + mysql 进行开发
棱镜版本是 2.28,
我在使用 prisma 时遇到问题。
当模型是
model User{
id Int @id @default(autoincrement())
email String @unique @db.VarChar(30)
password String? @db.VarChar(200)
nickname String? @unique @db.VarChar(30)
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
department String? @db.VarChar(50)
introduce String? @db.Text
createdAt DateTime
updatedAt DateTime?
user User @relation(fields: [userId], references: [id])
userId Int
wellTalent WellTalent[]
interestTalent InterestTalent[]
profileImage Image?
@@map(name: "profiles")
}
model InterestTalent {
id Int @id @default(autoincrement())
contents String?
createdAt DateTime
updatedAt DateTime?
profile Profile @relation(fields: [profileId], references: [id])
profileId Int
@@map(name: "interest_talents")
}
model WellTalent {
id Int @id @default(autoincrement())
contents String?
createdAt DateTime
updatedAt DateTime?
profile Profile @relation(fields: [profileId], references: [id])
profileId Int
@@map(name: "well_talents")
}
model Image {
id Int @id @default(autoincrement())
src String? @db.VarChar(200)
createdAt DateTime
updatedAt DateTime?
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int?
@@map(name: "images")
}
如果我想与用户表一起查找配置文件数据
const prisma = new PrismaClient();
const findByIdWithProfile = async (id) => {
try {
return await prisma.user.findUnique({
where: { id },
select: {
id: true,
nickname: true,
email: true,
profile: {
select: {
id: true,
department: true,
introduce: true,
wellTalent: {
select: {
contents: true,
},
},
interestTalent: {
select: {
contents: true,
},
},
profileImage: {
select: {
src: true,
},
},
},
},
},
});
} catch (err) {
console.error(err);
}
};
代码行非常增加..
我可以分别找到 User 表和 Profile 表,但使用嵌套查询来最小化 DB 访问。
项目越大,情况就越糟糕。
好像是DB结构结构不对,但是替换成本已经太高了,不知道哪种方式最好。
感谢您的帮助
【问题讨论】:
-
嗨,您能否澄清一下返回非常具体的字段选择是否是一项要求?如果没有必要,那么您可以使用
include而不是select返回某个关系的所有字段。如果这是一项重要要求,那么恐怕您将不得不像现在一样手动指定字段。您可以做的最好的事情是通过将select语句的多个键值对放在同一行中,使代码不那么冗长且更易于阅读。 -
首先,感谢您的评论。使用
select不是一个重要的要求。我只是用它来获取特定数据而不返回需要的id关系(用户表中的密码不同),如果没有要求,使用include是否正确?我认为代码会比select更简洁。我只是想知道是否有 Prisma 提供的功能,或者是否有另一种 db 级别的方法! @TasinIshmam -
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。