如何解决Prisma findMany 函数不返回关系数据
我正在尝试使用 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' }
]
产品数据库
我不知道为什么我的 findMany() 没有重新调整关系数据库数据。谢谢
解决方法
我建议阅读文档,尤其是 this chapter 和后面的文档。在他们的示例中,它们有一个 Author
和 Post
,其中 Post
有一个指向 author
的 Author
字段。他们的代码如下所示:
const getPosts = await prisma.post.findMany({
where: {
title: {
contains: 'cookies',},include: {
author: true,// Return all fields
},})
稍低一点的“包含深层嵌套关系”一章实际上使用了一个字段 .category
,它也是一个数组,就像您使用 .transactions
的示例一样。
您需要将 include 显式设置为 true。
所以你的代码看起来像这样
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card,Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request,response) => {
const agent = new WebhookClient({ request,response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
let intentMap = new Map();
intentMap.set('Default Welcome Intent',welcome);
intentMap.set('Default Fallback Intent',fallback);
function welcome(agent){
agent.add("What is your favorite animal?");
agent.add(new Suggestion("Dog"));
agent.add(new Suggestion("Cat"));
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry,can you try again?`);
}
agent.handleRequest(intentMap);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。