我正在尝试使用Prisma Client查询Prisma模型的属性。该模型是一个餐厅模型,具有reviews
属性。reviews
属性也与Review
模型相关。
schema.prisma
文件:
// 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客户端查询:
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和实验应用程序目录,并在Postgres上使用Prisma构建ORM。
更新:
我能够删除node_modules
并运行npm install
来恢复它。然而,如果我控制台restaurants.reviews
,我得到undefined。当控制台restaurants`` variable the
评论property returns
评论时:[ [Object] ]```。
1条答案
按热度按时间6ss1mwsb1#
Review
模型后没有进行迁移。从here开始
db push使用与Prisma Migrate相同的引擎来同步您的Prisma模式与数据库模式。db push命令:
1-内省数据库以推断并执行使数据库模式反映Prisma模式状态所需的更改。
2-默认情况下,将更改应用到数据库架构后,将触发生成器(例如Prisma Client)。您不需要手动调用Prisma generate。
prisma push
有关。如果您将鼠标悬停在restaurants
上,它的类型应该会在模态中弹出,并与IRestaurantCardType
进行比较。它的类型将不匹配