我试图用Prisma客户端查询Prisma模型的属性。该模型是一个具有reviews
属性的餐厅模型。reviews
性质也与Review
模型有关。
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
}
我正在尝试使用Prisma客户端从page.tsx
文件查询此模式。
查询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;
};
然而,上面的代码产生了两个错误。第一个错误在import语句中;具体来说,尝试导入Review
类型。Module '"@prisma/client"' has no exported member 'Review'.ts(2305)
其他导入都不会产生此错误。
另一个错误发生在fetchRestaurants
函数中。特别是在reviews: true,
的select
属性中的restaurants
对象中。
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)
我使用Next 13与实验应用程序目录和Prisma为我的ORM构建在Postgres上。
更新:
我能够删除node_modules
并运行npm install
来恢复它。这就修正了两个错误;然而,如果我控制台restaurants.reviews
我得到未定义。在安慰restaurants`` variable the
评论时,property returns
评论:[[Object]] ' ' ' .
-
第一个错误可能是因为您在添加
Review
模型后没有迁移。你应该运行npx prisma db push
从这里
db push使用与Prisma Migrate相同的引擎来同步prism模式与您的数据库模式。db push命令:
1-自省数据库以推断和执行所需的更改使你的数据库模式反映你的Prisma模式的状态。
2-默认情况下,在对数据库模式应用更改后,生成器被触发(例如,Prisma Client)。你不需要手动调用prisma generate.
-
你有prism生成类型,你需要运行
npx prisma generate
则需要重新启动服务器