gpt4 book ai didi

javascript - Prisma findMany 函数不返回关系数据

转载 作者:行者123 更新时间:2023-12-05 01:58:18 24 4
gpt4 key购买 nike

我正在尝试使用 Prisma 2.28.0 用关系数据填充我的数据,这是我的

下面的 Schema.prisma 模型

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Product {
id Int @id @default(autoincrement())
name String @db.VarChar(255)
transactions Transaction[]
}

model Transaction {
id BigInt @id @default(autoincrement())
quantity Int
time Int
product Product? @relation(fields: [productId], references: [id])
productId Int?
}


我正在尝试获取数据的函数。

const { PrismaClient }=require("@prisma/client")

const prisma = new PrismaClient()

async function checkPrismaConnection(){
try {
const result=await prisma.product.findMany();
console.log(result);
}catch (e) {
console.log(e);
}
}

checkPrismaConnection();

输出

[
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Masum' },
{ id: 3, name: 'Rezaul' }
]

交易数据库结果 enter image description here

产品数据库

enter image description here

我不知道为什么我的 findMany() 没有重新调整关系数据库数据。谢谢

最佳答案

我建议阅读文档,尤其是 this chapter以及随后的那些。在他们的示例中,他们有一个 AuthorPost,其中 Post 有一个 author 字段指向一个 作者。他们的代码如下所示:

const getPosts = await prisma.post.findMany({
where: {
title: {
contains: 'cookies',
},
},
include: {
author: true, // Return all fields
},
})

“包含深层嵌套关系”一章实际上使用了一个字段 .category ,它也是一个数组,如您使用 .transactions 的示例。

关于javascript - Prisma findMany 函数不返回关系数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68610423/

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