Prisma2 Error: Invalid `prisma.post.create()` invocation: Unknown arg `tags` in data.tags for type PostUncheckedCreateInput

Viewed 3044

I want to create a post with a list of tags attached to it. The models are connected many-to-many (one post can have several tags, and one tag can have several posts in it).

Here are my prisma models:

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
}

And here's a mutation where I'm trying to create a post, and attach tags to it:

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.
      },
    })
  },
})

This doesn't seem to be working.

I'm getting an error:

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
}

It seems like the tags field on the post is missing? But I did run prisma generate and prisma migrate. Also I can successfully query tags on a post if I add them manually using Prisma Studio. What could be causing this issue?

2 Answers

You need to use connect for the author as well. So the following will work fine:

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.
      },
})

In my case, the issue arose when I created a new field on the prisma model called uid and tried to run the command prisma migrate dev

It brought the error

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.

I solved it by adding the @default("") to it.

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
}
Related