Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
4 replies
jack

Help with prisma nested create

I have this prisma model
model Membership {
    userId  String
    groupId String
    User    User   @relation(fields: [userId], references: [id])
    Group   Group  @relation(fields: [groupId], references: [id])

    @@id([userId, groupId])
}
, and I'm trying to create a Membership entry where the group is created nested. The reason for this is that, when I'm creating a new group, I need to also create a Membership entry. Instead of in one query creating the group, and in another creating the membership, I thought I would nest the group creation within the membership creation. I'm trying something along these lines
      const groupResource = await ctx.prisma.membership.create({
        data: {
          userId: id,
          Group: {
            create: {
              title: title,
              slug: slugify(title),
              ownerId: id,
            }
          }
        },
      });
but am getting a long error, which I think roots from
Types of property 'userId' are incompatible.
        Type 'string' is not assignable to type 'undefined'.ts(2322)
Was this page helpful?