Mathias Riis Sorensen
Mathias Riis Sorensen

Reputation: 850

How to connect to more than one relation using Prisma Client upsert function?

When I'm trying to connect more than one relation to my Profile table I get the following error message:

PrismaClientValidationError:
Invalid `prisma.connection.upsert()` invocation:

{
  where: {
    id: ''
  },
  update: {
    userOneId: 'clbcb6z58000gyb31ez95frvh',
    userTwoId: 'clbcaz4bl000ayb31icwpzphu',
    isUserOneApproved: false,
    isUserTwoApproved: true,
    connectionRequestedOnDate: '2022-12-07T21:48:12.441Z',
    connectionAcceptedOnDate: undefined
  },
  create: {
    userOneId: 'clbcb6z58000gyb31ez95frvh',
    ~~~~~~~~~~~~~
    userTwoId: 'clbcaz4bl000ayb31icwpzphu',
    ~~~~~~~~
    isUserOneApproved: false,
    isUserTwoApproved: true,
    connectionRequestedOnDate: '2022-12-07T21:48:12.441Z',
    connectionAcceptedOnDate: undefined,
    userOne: {
      connect: {
        id: 'clbcb6z58000gyb31ez95frvh'
      }
    },
    userTwo: {
      connect: {
        id: 'clbcaz4bl000ayb31icwpzphu'
      }
    },
    profile: {
      connect: {
        id: 'clbcb71l2000kyb31cclk79z3'
      }
    }
  }
}

Unknown arg `userOneId` in create.userOneId for type ConnectionCreateInput. Did you mean `userOne`? Available args:
type ConnectionCreateInput {
  id?: String
  isUserOneApproved: Boolean
  isUserTwoApproved: Boolean
  connectionRequestedOnDate?: DateTime | Null
  connectionAcceptedOnDate?: DateTime | Null
  userOne: ProfileCreateNestedOneWithoutUserOneInput
  userTwo: ProfileCreateNestedOneWithoutUserTwoInput
  profile: ProfileCreateNestedOneWithoutConnectionInput
}
Unknown arg `userTwoId` in create.userTwoId for type ConnectionCreateInput. Did you mean `userTwo`? Available args:
type ConnectionCreateInput {
  id?: String
  isUserOneApproved: Boolean
  isUserTwoApproved: Boolean
  connectionRequestedOnDate?: DateTime | Null
  connectionAcceptedOnDate?: DateTime | Null
  userOne: ProfileCreateNestedOneWithoutUserOneInput
  userTwo: ProfileCreateNestedOneWithoutUserTwoInput
  profile: ProfileCreateNestedOneWithoutConnectionInput
}

This is how my prisma upsert function is currently defined:

 const createOrUpdateRole = await prisma.connection.upsert({
      where: { id: id },
      update: {
        userOneId: userOneId,
        userTwoId: userTwoId,
        isUserOneApproved,
        isUserTwoApproved,
        connectionRequestedOnDate,
        connectionAcceptedOnDate,
      },
      create: {
        userOneId: userOneId,
        userTwoId: userTwoId,
        isUserOneApproved,
        isUserTwoApproved,
        connectionRequestedOnDate,
        connectionAcceptedOnDate,
        userOne: { connect: { id: userOneId } },
        userTwo: { connect: { id: userTwoId } },
        profile: { connect: { id: profileId } },
      },
    });

My expectation when running the function was to connect userOne and userTwo to my Profile table using their respective ids. The reason for that was that I could then query for the connection and then read the profile data for userOne and userTwo.

This is my models defined in schema.prisma:

model Profile {
  id                   String       @id @default(cuid()) 
  firstName            String?
  lastName             String?
  image                String?      @db.Text  
  userId               String
  user                 User         @relation(fields: [userId], references: [id], onDelete: Cascade)
  sensitive            Sensitive[]
  userOne              Connection[] @relation("userOne")
  userTwo              Connection[] @relation("userTwo")
  connection           Connection[] @relation("profile")

  @@index([userId])
}
model Connection {
  id String @id @default(cuid())
  isUserOneApproved         Boolean
  isUserTwoApproved         Boolean
  connectionRequestedOnDate DateTime?
  connectionAcceptedOnDate  DateTime?
  userOneId                 String
  userOne                   Profile   @relation("userOne", fields: [userOneId], references: [id], onDelete: Cascade)
  userTwoId                 String
  userTwo                   Profile   @relation("userTwo", fields: [userTwoId], references: [id], onDelete: Cascade)
  profileId                 String
  profile                   Profile   @relation("profile", fields: [profileId], references: [id], onDelete: Cascade)

  @@index([userOneId])
  @@index([userTwoId])
  @@index([profileId])
}

If I decide to one connect to only one of them, for instance userOneId using userOne: { connect: { id: userOneId } }, then the function runs as expected, but as soon as I start to define more than one I get the error mentioned above. What have I missed?

Upvotes: 0

Views: 1551

Answers (1)

Shea Hunter Belsky
Shea Hunter Belsky

Reputation: 3218

The error is pretty self-descriptive:

Unknown arg `userOneId` in create.userOneId for type ConnectionCreateInput

Since you are already passing userOne: {connect: {id: '.....' } } in your Prisma operation, you do not (and should not) also pass userOneId. You just need userOne: {connect..... and userTwo: {connect......

Your operation will end up looking like this:

const createOrUpdateRole = await prisma.connection.upsert({
    where: {
        id: id
    },
    update: {
        // You can have __either__ `userOneId: 'value'` or `userOne: {connect....` here
        // but not both
        userOneId: userOneId,
        userTwoId: userTwoId,
        isUserOneApproved,
        isUserTwoApproved,
        connectionRequestedOnDate,
        connectionAcceptedOnDate,
    },
    create: {
        isUserOneApproved,
        isUserTwoApproved,
        connectionRequestedOnDate,
        connectionAcceptedOnDate,
        userOne: {
            connect: {
                id: userOneId
            }
        },
        userTwo: {
            connect: {
                id: userTwoId
            }
        },
        profile: {
            connect: {
                id: profileId
            }
        },
    },
});

Upvotes: 1

Related Questions