【问题标题】:Invalid prisma.model.create invocation in. Unknown arg `school_id` in data.subjects.create.2.school_id for type CreateWithoutInout无效的 prisma.model.create 调用。 data.subjects.create.2.school_id 中类型 CreateWithoutInout 的未知 arg `school_id`
【发布时间】:2022-11-14 12:55:43
【问题描述】:

我有这两个型号

model School {
  id         Int           @id @default(autoincrement())
  name       String?
  center_no  Int?
  type       String?
  subjects   SubjectPool[]
}
model SubjectPool {
  id   Int     @id @default(autoincrement())
  name String?
  code String?

  school    School?         @relation(fields: [school_id], references: [id])
  school_id Int?
}

我正在尝试使用 seed.ts 文件中的此查询为数据库播种

 await prisma.school.create({
        data: {
            ...school,
            subjects: {
                create: [ math, english, geography]
            }
        },
        include: {
            subjects: true
        }

    })

但问题是我得到了这个错误:


Unknown arg `school_id` in data.subjects.create.1.school_id for type SubjectPoolUncheckedCreateWithoutSchoolInput. Did you mean `class_id`? Available args:     
type SubjectPoolUncheckedCreateWithoutSchoolInput {
  id?: Int
  name?: String | Null
  code?: String | Null
  class_id?: Int | Null
}

似乎在我认为存在的主题池模型上找不到学校 ID 字段。

这是我在遇到此错误之前运行的命令序列:

dropdb dbname
createdb dbname
npx prisma db push / npx prisma migrate dev (if i have made a change to the model)
npx prisma db seed

我正在使用 postgres 数据库。完整的错误:

 await prisma.school.create({
       data: {
         id: 1,
         name: 'kansenshi secondary school',
         center_no: 2783783,
         type: 'gov',
         province: 'copperbelt',
         district: 'Ndola',
         subjects: {
           create: [
             null,
             {
               id: 3,
               name: 'English',
               code: '234',
               school_id: null,
               ~~~~~~~~~
               class_id: null
             },
             null
           ]
         }
       },
       include: {
         subjects: true
       }
     })

Unknown arg `school_id` in data.subjects.create.1.school_id for type SubjectPoolUncheckedCreateWithoutSchoolInput. Did you mean `class_id`? Available args:     
type SubjectPoolUncheckedCreateWithoutSchoolInput {
  id?: Int
  name?: String | Null
  code?: String | Null
  class_id?: Int | Null
  schedule?: ClassScheduleUncheckedCreateNestedManyWithoutSubjectInput
}

【问题讨论】:

    标签: node.js reactjs next.js graphql prisma


    【解决方案1】:

    该错误表明您正在种子文件中添加字段school_idschool_id 引用 SubjectPool 模型的 id 字段,因此在您创建 subject 关系时将为您创建它。还查看错误日志,您有一些字段provincedistrict 在您共享的学校模型中未指定。要修复您的错误,您可以使用我根据您共享的模型创建的种子数据。

    await prisma.school.create({
        data: {
          name: 'School 1',
          center_no: 123456,
          type: 'gov',
          subjects: {
            create: [
              {
                name: 'Math',
                code: 'MATH',
              },
              {
                name: 'Science',
                code: 'SCI',
              },
            ],
          },
        },
      });
    

    在使用 Prisma 的关系查询中查看Nested Writes 上的文档。

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 2015-06-21
      • 2020-11-02
      • 2020-03-29
      • 1970-01-01
      • 2022-07-06
      • 2017-03-08
      • 2021-06-02
      • 2016-11-18
      相关资源
      最近更新 更多