You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prisma MongoDB入门更新Post报错:'comments'属性不存在

问题:Prisma MongoDB更新Post时出现TypeScript编译错误

按照Prisma MongoDB官方教程操作,已成功添加数据并确认数据库连接,但在更新Post的最后步骤中出现以下TypeScript编译错误,无法完成教程。已查阅Prisma的GitHub Issues但未找到同类问题,请问可能的原因是什么?

报错信息

return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
index.ts:20:11 - error TS2322: Type '{ comments: { createMany: { data: { comment: string; }[]; }; }; }' is not assignable to type '(Without<PostUpdateInput, PostUncheckedUpdateInput> & PostUncheckedUpdateInput) | (Without<...> & PostUpdateInput)'.
  Object literal may only specify known properties, and 'comments' does not exist in type '(Without<PostUpdateInput, PostUncheckedUpdateInput> & PostUncheckedUpdateInput) | (Without<...> & PostUpdateInput)'.

 20           comments: {
              ~~~~~~~~~~~
 21             createMany: {
    ~~~~~~~~~~~~~~~~~~~~~~~~~
...
 26             },
    ~~~~~~~~~~~~~~
 27           },
    ~~~~~~~~~~~

// The similar error message follows below for 
comments: true,

相关代码

index.ts代码

import { PrismaClient } from "@prisma/client"

const prisma = new PrismaClient()

main()
    .then(async () => {
        await prisma.$disconnect()
    })
    .catch(async (e) => {
        console.error(e)
        await prisma.$disconnect()
        process.exit(1)
    })
async function main() {
    await prisma.post.update({
        where: {
            slug: "my-first-post",
        },
        data: {
            comments: {
                createMany: {
                    data: [
                        { comment: "Great post!" },
                        { comment: "Can't wait to read more!" },
                    ],
                },
            },
        },
    })
    const posts = await prisma.post.findMany({
        include: {
            comments: true,
        },
    })

    console.dir(posts, { depth: Infinity })
}

prisma.schema配置(直接复制自教程)

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model Post {
  id       String    @id @default(auto()) @map("_id") @db.ObjectId
  slug     String    @unique
  title    String
  body     String
  author   User      @relation(fields: [authorId], references: [id])
  authorId String    @db.ObjectId
}

model User {
  id      String   @id @default(auto()) @map("_id") @db.ObjectId
  email   String   @unique
  name    String?
  address Address?
  posts   Post[]
}

// Address is an embedded document
type Address {
  street String
  city   String
  state  String
  zip    String
}

环境信息

Environment variables loaded from .env
prisma                  : 4.8.1
@prisma/client          : 4.8.1
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Format Wasm             : @prisma/prisma-fmt-wasm 4.8.0-61.d6e67a83f971b175a593ccc12e15c4a757f93ffe
Default Engines Hash    : d6e67a83f971b175a593ccc12e15c4a757f93ffe
Studio                  : 0.479.0

问题原因与解决方案

  • 核心原因:你的prisma.schema中缺失Comment模型定义,且Post模型未配置与Comment的关联关系。教程的更新步骤默认已存在Comment模型,但你复制的schema遗漏了这部分内容,导致Prisma Client生成的类型中没有comments字段,触发TypeScript编译错误。
  • 解决步骤:
    1. 在prisma.schema中添加Comment模型,并建立与Post的关联:
      model Comment {
        id      String @id @default(auto()) @map("_id") @db.ObjectId
        comment String
        post    Post   @relation(fields: [postId], references: [id])
        postId  String @db.ObjectId
      }
      
    2. 在Post模型中添加comments关联字段:
      model Post {
        id       String    @id @default(auto()) @map("_id") @db.ObjectId
        slug     String    @unique
        title    String
        body     String
        author   User      @relation(fields: [authorId], references: [id])
        authorId String    @db.ObjectId
        comments Comment[] // 新增该行,定义Post与Comment的一对多关联
      }
      
    3. 重新生成Prisma Client,执行命令:npx prisma generate
    4. 重新运行代码即可解决编译错误

内容的提问来源于stack exchange,提问作者iron7874

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 04:45:33