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

j8ag8udp  于 2023-04-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(376)

我正在尝试使用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] ]```。

6ss1mwsb

6ss1mwsb1#

  • 第一个错误可能是因为您在添加Review模型后没有进行迁移。
npx prisma db push

here开始
db push使用与Prisma Migrate相同的引擎来同步您的Prisma模式与数据库模式。db push命令:
1-内省数据库以推断并执行使数据库模式反映Prisma模式状态所需的更改。
2-默认情况下,将更改应用到数据库架构后,将触发生成器(例如Prisma Client)。您不需要手动调用Prisma generate。

  • 我认为第二个错误也与prisma push有关。如果您将鼠标悬停在restaurants上,它的类型应该会在模态中弹出,并与IRestaurantCardType进行比较。它的类型将不匹配

相关问题