I am trying to populate my data with relational data using Prisma 2.28.0, Here is my
Schema.prisma model below
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?
}
the function I am trying to fetch data.
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();
OutPut
[
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Masum' },
{ id: 3, name: 'Rezaul' }
]
Product DB
I don't know why my findMany() is not retuning relational db data. Thank you
const get = await prisma.post.findMany({ where: { title: { contains: 'cookies', },}, include: { author: true, // Return all fields },
}) The "Including Deep Nested Relationships" chapter a bit further down actually uses a .category field that is also an array, as in your example with .transactions .
Clean must set include to true.
So the code will look like this for you
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);
const get = await prisma.post.findMany({ where: { title: { contains: 'cookies', },}, include: { author: true, // Return all fields },
}
}
checkPrismaConnection();