使用Prisma客户端无法访问Prisma模型?



我试图用Prisma客户端查询Prisma模型的属性。该模型是一个具有reviews属性的餐厅模型。reviews性质也与Review模型有关。

schema.prismafile:

// 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
    

则需要重新启动服务器

相关内容

  • 没有找到相关文章

最新更新