gpt4 book ai didi

javascript - Prisma 客户端从表中选择所有行

转载 作者:行者123 更新时间:2023-12-05 04:29:21 25 4
gpt4 key购买 nike

如何使用 prisma 从表格中选择所有内容? (从应用程序中选择 *)

const applications = prisma.application.findMany({
// Returns all user fields
include: {
posts: {
select: {
age: true,
about_section: true,
user_id: true
},
},
},
})
console.log(applications.age)

这是我的模式的样子:

model application {
application_id Int @id @default(autoincrement())
age String? @db.VarChar(255)
about_section String? @db.VarChar(255)
user_id Int?
users users? @relation(fields: [user_id], references: [user_id], onDelete: Restrict, onUpdate: Restrict, map: "application_ibfk_1")

@@index([user_id], map: "user_id")
}

最佳答案

对于行,不带wherefindMany() 将返回表中的所有行:

Get all Records
以下 findMany 查询返回所有用户记录:

const users = await prisma.user.findMany()

对于列/字段,在不指定includeselect 的情况下,prisma 有一个default :

By default, when a query returns records (as opposed to a count), theresult includes the default selection set:

  • All scalar fields defined in the Prisma schema (including enums)
  • None of the relations

对于SELECT * FROM application(不包括users关系):

const applications = await prisma.application.findMany();

进一步在模型中包含users关系:

const applications = await prisma.application.findMany({
include: {
users: true // will include all fields
}
});

关于javascript - Prisma 客户端从表中选择所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72339652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com