#Making a blog in order to learn Remix, having issues with prisma query for many-to-many relationship

1 messages · Page 1 of 1 (latest)

thick musk
#

I have the following schema:

model Post {
  id           Int @id @default(autoincrement())
  slug         String @unique
  title        String
  markdown     String
  postImageUrl String
  ogLocale     String @default("en_US")
  tags         TagsOnPosts[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
  userId String
}

model Tag {
  id   Int @id @default(autoincrement())
  name String
  posts TagsOnPosts[]
}

model TagsOnPosts {
  post       Post @relation(fields: [postId], references: [id])
  postId     Int
  tag        Tag @relation(fields: [tagId], references: [id])
  tagId      Int
  assignedAt DateTime @default(now())
  assignedBy String

  @@id([postId, tagId])
}

And the following code in order to insert a new post:

export async function createPost(
  post: Pick<Post, "slug" | "title" | "markdown" | "postImageUrl">,
  tags: string[],
  userId: string
) {
  const tagsCreate = tags.map((tag) => {
    return {
      tag: {
        connectOrCreate: {
          where: { name: tag },
          create: { name: tag, id: null },
        }
      },
      assignedBy: userId
    }
  });

  return prisma.post.create({
    data: {
      slug: post.slug,
      title: post.title,
      markdown: post.markdown,
      postImageUrl: post.postImageUrl,
      userId: userId, // provide the userId here
      tags: {
        create: tagsCreate
      }
    }
  });
}

Posting the rest as a comment..

#

Also when trying to run it anyway (since it builds fine) it generates the following error:

Error: 
Invalid `prisma.post.create()` invocation in
blog\app\models\post.server.ts:28:22
Unknown arg `name` in data.tags.create.0.tag.connectOrCreate.where.name for type TagWhereUniqueInput. Did you mean `id`? Available args:
type TagWhereUniqueInput {
  id?: Int
}
Unknown arg `id` in data.tags.create.0.tag.connectOrCreate.create.id for type TagCreateWithoutPostsInput. Available args:

type TagCreateWithoutPostsInput {
  name: String
}
Unknown arg `name` in data.tags.create.1.tag.connectOrCreate.where.name for type TagWhereUniqueInput. Did you mean `id`? Available args:
type TagWhereUniqueInput {
  id?: Int
}
Unknown arg `id` in data.tags.create.1.tag.connectOrCreate.create.id for type TagCreateWithoutPostsInput. Available args:

#

Having typescript complain on the create part in the tags with the following message:

TS2322: Type '{ tag: { connectOrCreate: { where: { name: string; }; create: { name: string; id: null; }; }; }; assignedBy: string; }[]' is not assignable to type '(Without<TagsOnPostsCreateWithoutPostInput, TagsOnPostsUncheckedCreateWithoutPostInput> & TagsOnPostsUncheckedCreateWithoutPostInput) | ... 5 more ... | undefined'.   Type '{ tag: { connectOrCreate: { where: { name: string; }; create: { name: string; id: null; }; }; }; assignedBy: string; }[]' is not assignable to type 'Without<TagsOnPostsUncheckedCreateWithoutPostInput[], TagsOnPostsCreateWithoutPostInput[]> & TagsOnPostsCreateWithoutPostInput[]'.     Type '{ tag: { connectOrCreate: { where: { name: string; }; create: { name: string; id: null; }; }; }; assignedBy: string; }[]' is not assignable to type 'TagsOnPostsCreateWithoutPostInput[]'.       Type '{ tag: { connectOrCreate: { where: { name: string; }; create: { name: string; id: null; }; }; }; assignedBy: string; }' is not assignable to type 'TagsOnPostsCreateWithoutPostInput'.         The types of 'tag.connectOrCreate.create' are incompatible between these types.           Type '{ name: string; id: null; }' is not assignable to type '(Without<TagCreateWithoutPostsInput, TagUncheckedCreateWithoutPostsInput> & TagUncheckedCreateWithoutPostsInput) | (Without<...> & TagCreateWithoutPostsInput)'.             Type '{ name: string; id: null; }' is not assignable to type 'Without<TagUncheckedCreateWithoutPostsInput, TagCreateWithoutPostsInput> & TagCreateWithoutPostsInput'.               Type '{ name: string; id: null; }' is not assignable to type 'Without<TagUncheckedCreateWithoutPostsInput, TagCreateWithoutPostsInput>'.                 Types of property 'id' are incompatible.                   Type 'null' is not assignable to type 'undefined'.

What I want to happen is to connect existing tags to the post or create new ones if they dont exist

crude turret
#

Unknown arg 'name' in data.tags.create.0.tag.connectOrCreate.where.name for type TagWhereUniqueInput This is because the where clause in connectOrCreate needs to uniquely identify the element you want to connect to. Since name doesn't uniquely identify a tag, you cannot use it as a parameter in connectOrCreate.where.

Unknown arg 'id' in data.tags.create.1.tag.connectOrCreate.create.id for type TagCreateWithoutPostsInput This is because its being specified as null. It needs to be left undefined, so that prisma/the db can autogenerate the id for it