Reputation: 1676
I'm trying to query a property of a Prisma model with Prisma Client. The model is a restaurant model which has a reviews
property. The reviews
property is also related to a Review
model.
schema.prisma
file:// This is your Prisma schema file,
// learn more about it in the docs: 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[]
}
model Item {
id Int @id @default(autoincrement())
name String
price String
description String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
restaurant_id Int
restaurant Restaurant @relation(fields: [restaurant_id], references: [id])
}
model Location {
id Int @id @default(autoincrement())
name String
restraunts Restaurant[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
model Cuisine {
id Int @id @default(autoincrement())
name String
restraunts Restaurant[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
model User {
id Int @id @default(autoincrement())
first_name String
last_name String
city String
email String
password String
phone String
review Review[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
model Review {
id Int @id @default(autoincrement())
first_name String
last_name String
rating RATING
text String
restaurant_id Int
restaurant Restaurant @relation(fields: [restaurant_id], references: [id])
user_id Int
user User @relation(fields: [user_id], references: [id])
created_at DateTime @default(now())
updated_at DateTime @updatedAt
}
enum PRICE {
CHEAP
REGULAR
HIGH
EXPENSIVE
}
enum RATING {
HALF
ONE
ONE_HALF
TWO
TWO_HALF
THREE
THREE_HALF
FOUR
FOUR_HALF
FIVE
}
I'm trying to query this schema from a page.tsx
file using 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, the above code produces two errors. The first error is in the import statement; specifically, trying to import the Review
type.
Module '"@prisma/client"' has no exported member 'Review'.ts(2305)
None of the other imports produce this error.
The other error happens in the fetchRestaurants
function. Specifically in the restaurants
object 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'm using Next 13 with the experimental app directory and Prisma for my ORM building on Postgres.
I was able to delete the node_modules
and run npm install
to restore it. That fixed the two errors; however, if I console restaurants.reviews
I get undefined. And when consoling out the restaurants`` variable the
reviewsproperty returns
reviews: [ [Object] ]```.
Upvotes: 1
Views: 2567
Reputation: 49661
First error might be because you did not migrate after adding Review
model. you should run
npx prisma db push
From here
db push uses the same engine as Prisma Migrate to synchronize your Prisma schema with your database schema. The db push command:
1- Introspects the database to infer and executes the changes required to make your database schema reflect the state of your Prisma schema.
2- By default, after changes have been applied to the database schema, generators are triggered (for example, Prisma Client). You do not need to manually invoke prisma generate.
yo have prisma generate types, you need to run
npx prisma generate
then you need to restart the server
Upvotes: 3