I'm currently attempting to retrieve a specific property of a Prisma model using Prisma Client. The model in question is related to restaurants and includes a reviews
property that also corresponds with a separate Review
model.
schema.prisma
file:
// This here is your Prisma schema document,
// read further about it in the documentation: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Restaurant {
id Int @id @default(autoincrement())
name String
main_img String
images String[]
description String
price PRICE
opens_at String
closes_at String
slug String @unique
created_at DateTime @default(now())
updated_at DateTime @updatedAt
item Item[]
location_id Int @unique
location Location @relation(fields: [location_id], references: [id])
cuisine_id Int @unique
cuisine Cuisine @relation(fields: [cuisine_id], references: [id])
review_id Int @unique
reviews Review[]
}
... (repeated various other schemas)
enum RATING {
HALF
ONE
ONE_HALF
TWO
TWO_HALF
THREE
THREE_HALF
FOUR
FOUR_HALF
FIVE
}
The objective is to execute queries on this schema from a page.tsx
file by leveraging the Prisma client.
Interacting with Prisma Client:
import { PrismaClient, Cuisine, Location, PRICE, Review } from "@prisma/client";
const prisma = new PrismaClient();
export interface IRestaurantCardType {
id: number;
name: string;
price: PRICE;
main_img: string;
location: Location;
cuisine: Cuisine;
slug: string;
reviews: Review[];
}
const fetchRestaurants = async (): Promise <IRestaurantCardType[]> => {
const restaurants = await prisma.restaurant.findMany({
select: {
id: true,
name: true,
price: true,
main_img: true,
location: true,
cuisine: true,
slug: true,
reviews: true,
}
});
return restaurants;
};
However, there are two issues arising from the provided code. The first problem involves an error within the import declaration, specifically when attempting to import the Review
type.
Module '"@prisma/client"' has no exported member 'Review'.ts(2305)
The remainder of the imports do not trigger this particular error.
The second issue occurs within the fetchRestaurants
function, specifically pertaining to the object restaurants
in the select
property at reviews: true,
.
Type '{ id: true; name: true; price: true; main_img: true; location: true; cuisine: true; slug: true; reviews: true; }' is not assignable to type 'RestaurantSelect'.
Object literal may only specify known properties, and 'reviews' does not exist in type 'RestaurantSelect'.ts(2322)
I am employing Next.js 13 with the experimental app directory structure in combination with Prisma for my ORM operations based on Postgres.
Update:
I managed to remove the node_modules
directory and reinstated it through running npm install
, thereby resolving both errors. Nonetheless, if I attempt to log restaurants.reviews
, it returns as undefined. Additionally, inspecting the restaurants
variable reveals the reviews
property returning as reviews: [ [Object] ]
.