【问题标题】:Prisma2 Error: Invalid `prisma.post.create()` invocation: Unknown arg `tags` in data.tags for type PostUncheckedCreateInputPrisma2 错误:无效的 `prisma.post.create()` 调用:类型 PostUncheckedCreateInput 的 data.tags 中的未知 arg `tags`
【发布时间】:2021-09-13 20:43:39
【问题描述】:

我想创建一个附有标签列表的帖子。模型是多对多连接的(一个帖子可以有多个标签,一个标签可以有多个帖子)。

这是我的棱镜模型:

model Post {
  id String @id @default(cuid())
  slug String @unique
  title String
  body String
  tags Tag[]
}

model Tag {
  id String @id @default(cuid())
  posts Post[]
  name String
  slug String @unique
}

这是一个突变,我正在尝试创建一个帖子,并为其附加标签:

t.field('createPost', {
  type: 'Post',
  args: {
    title: nonNull(stringArg()),
    body: stringArg(),
    tags: list(arg({ type: 'TagInput' }))
  },
  resolve: async (_, args, context: Context) => {
    // Create tags if they don't exist
    const tags = await Promise.all(
      args.tags.map((tag) =>
        context.prisma.tag.upsert({
          create: omit(tag, "id"),
          update: tag,
          where: { id: tag.id || "" },
        })
      )
    )
    return context.prisma.post.create({
      data: {
        title: args.title,
        body: args.body,
        slug: `${slugify(args.title)}-${cuid()}`,
        // Trying to connect a post to an already existing tag
        // Without the "tags: {...} everything works
        tags: {
          set: [{id:"ckql6n0i40000of9yzi6d8bv5"}]
        },
        authorId: getUserId(context),
        published: true, // make it false once Edit post works.
      },
    })
  },
})

这似乎不起作用。

我收到一个错误:

Invalid `prisma.post.create()` invocation:
{
  data: {
    title: 'Post with tags',
    body: 'Post with tags body',
    slug: 'Post-with-tags-ckql7jy850003uz9y8xri51zf',
    tags: {
      connect: [
        {
          id: 'ckql6n0i40000of9yzi6d8bv5'
        }
      ]
    },
  }
}
Unknown arg `tags` in data.tags for type PostUncheckedCreateInput. Available args:
type PostUncheckedCreateInput {
  id?: String
  title: String
  body: String
  slug: String
}

帖子上的tags 字段似乎丢失了?但我确实运行了prisma generateprisma migrate。如果我使用 Prisma Studio 手动添加标签,我也可以成功查询帖子上的标签。什么可能导致此问题?

【问题讨论】:

  • 您最终找到解决方案了吗?我现在遇到了完全相同的问题,我无法弄清楚出了什么问题。
  • 没关系。我发现了我的问题,它与 Prisma 无关。我的构建过程出了点问题,它没有更新我的构建文件夹中的文件。删除我的整个构建文件夹并强制完全重建解决了我的问题。

标签: graphql nexus prisma prisma2 nexus-prisma


【解决方案1】:

您还需要将connect 用于author。所以以下将正常工作:

return context.prisma.post.create({
      data: {
        title: args.title,
        body: args.body,
        slug: `${slugify(args.title)}-${cuid()}`,
        // Trying to connect a post to an already existing tag
        // Without the "tags: {...} everything works
        tags: {
          set: [{id:"ckql6n0i40000of9yzi6d8bv5"}]
        },
        author: { connect: { id: getUserId(context) } },
        published: true, // make it false once Edit post works.
      },
})

【讨论】:

  • 不幸的是,这并没有解决问题,我仍然遇到同样的错误。我确实有作者和已发布的字段,并且按照您的描述设置它们,我刚刚从我的问题中删除了它们以使其更简洁,并避免将人们与额外不相关的字段混淆。
【解决方案2】:

在我的例子中,当我在 prisma 模型上创建一个名为 uid 的新字段并尝试运行命令 prisma migrate dev 时,问题就出现了

它带来了错误

Error:
⚠️ We found changes that cannot be executed:

  • Step 0 Added the required column `uid` to the `Transactions` table without a default value. There are 1 rows in this table, it is not possible to execute this step.

You can use prisma migrate dev --create-only to create the migration file, and manually modify it to address the underlying issue(s).
Then run prisma migrate dev to apply it and verify it works.

我通过添加@default("") 解决了它。

model Transactions {
  id Int @id @default(autoincrement())
  uid String @default("")
  account String
  description String
  category String
  reference String
  currency String @default("GBP")
  amount String
  status String
  transactionDate String
  createdAt String
  updatedAt String
}

【讨论】:

    猜你喜欢
    • 2022-11-14
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多