Jeremy Belolo
Jeremy Belolo

Reputation: 4541

Issue with nested creation in Prisma

Information about Prisma Schema, Client Queries and Environment

model Friend {
  id                 Int              @id @default(autoincrement())
  userId             Int
  friendId           Int
  addedAt            DateTime
  removedAt          DateTime?
  createdAt          DateTime         @default(now())
  updatedAt          DateTime         @updatedAt
  user               User             @relation("UserFriends", fields: [userId], references: [id])
  friendUser         User             @relation("FriendOfUsers", fields: [friendId], references: [id])

  @@unique([userId, friendId])
}

model User {
  id               Int               @id @default(autoincrement())
  name             String
  email            String?
  avatar           String?
  createdAt        DateTime          @default(now())
  updatedAt        DateTime          @updatedAt
  friends          Friend[]          @relation("UserFriends")
  friendOf         Friend[]          @relation("FriendOfUsers")
}

Take a look at the schema. Trying to create a Friend, while creating the FriendUser on the fly:

return await this.prisma.friend.upsert({
                    where: {
                        userId_friendId: {
                            userId: user.id,
                            friendId: friendUserId ?? 0
                        }
                    },
                    update: {
                        removedAt: null
                    },
                    create: {
                        userId: user.id,
                        alias: contact.name,
                        relationshipTypeId: (await this.getRelationshipType('Phone Number')).id,
                        addedAt: new Date(),
                        friendUser: {
                        create: {
                                name: contact.name,
                                avatar: contact.image
                            }
                        }
                    }
                });

But failing:

Type '{ userId: number; alias: string; relationshipTypeId: number; addedAt: Date; friendUser: { create: { name: string; avatar: string | null; }; }; }' is not assignable to type '(Without<FriendCreateInput, FriendUncheckedCreateInput> & FriendUncheckedCreateInput) | (Without<...> & FriendCreateInput)'.
  Types of property 'friendUser' are incompatible.
    Type '{ create: { name: string; avatar: string | null; }; }' is not assignable to type 'undefined'.

I guess it's because the type in the create only expects for scalars and not relations, but then how to achieve what I'm trying to do? Are Interactive Transactions the only way, creating the user beforehand?

Upvotes: 1

Views: 26

Answers (1)

Jeremy Belolo
Jeremy Belolo

Reputation: 4541

I made it work by typing a preparatory create variable with FriendCreateInput from the prisma client:

        const friendCreateData: Prisma.FriendCreateInput = {
            user: {
                connect: {
                    id: user.id
                }
            },
            friendUser: {
                connectOrCreate: {
                    where: { id: friendUser?.id ?? 0 },
                    create: {
                        name: contact.name
                    }
                }
            },
            addedAt: new Date(),
        };

                return await this.prisma.friend.upsert({
                    where: {
                        userId_friendId: {
                            userId: user.id,
                            friendId: friendUser?.id ?? 0
                        }
                    },
                    update: {
                        removedAt: null
                    },
                    create: friendCreateData
                });

Upvotes: 0

Related Questions