If I understand your question correctly, the request body may optionally contain an id or be undefined. If the id is present in the request, you want to update the corresponding record. If it isn't, you want to create a new record.

upsert functionality requires a unique identifier that can be used by the database to check whether there is a matching record. The database will then decide what to do. However with an undefined value, prisma cannot work.

You could use a key that will not exist in the database in case the value of id is undefined to make upsert insert the record in this case.

await prisma.profile.upsert({
  where: { id: id || '' },
  // ...
});

Or you could differentiate the cases and treat them appropriately:

if (id) {
  await prisma.profile.update({
    where: { id },
    data: {
      username,
      about,
      // ...
    },
  });
} else {
  await prisma.profile.create({
    data: {
      username,
      about,
      // ...
    },
  });
}

You might want to think about the case that there is an id given in the body, but it does exist in the database. Do you want the update to fail and reply with an error? Or do you want the database to silently insert? Then use upsert instead of update above.

Answer from some-user on Stack Overflow
🌐
Prisma
prisma.io › home › crud › crud › crud › crud
CRUD (Reference) | Prisma Documentation
const upsertUser = await prisma.user.upsert({ where: { email: "viola@prisma.io" }, update: { name: "Viola the Magnificent" }, create: { email: "viola@prisma.io", name: "Viola the Magnificent" }, });
Discussions

using upsert properly.
There was an error while loading. Please reload this page More on github.com
🌐 github.com
1
1
July 30, 2023
How is .upsertMany() implemented in Prisma ORM?
Prisma doesn't natively support upsertMany. More on stackoverflow.com
🌐 stackoverflow.com
how do I do this upsert query in prisma?
PrismaJoinThe official Discord server of Prisma! More on answeroverflow.com
🌐 answeroverflow.com
August 28, 2024
How to connect to more than one relation using Prisma Client upsert function? - Stack Overflow
This is how my prisma upsert function is currently defined: More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › prisma › prisma › discussions › 16882
Using upsert · prisma/prisma · Discussion #16882
Is there any way for me use upsert this way, or should I just use create and update. prisma.server.upsert({ where: { id: input.id }, update: input, create: { ...input, owner: { connect: { id: ctx.session.user.id } } } })
Author   prisma
🌐
Goprisma
goprisma.org › docs › walkthrough › upsert
Upsert records – Prisma Client Go
October 22, 2024 - post, err := client.Post.UpsertOne( // query db.Post.ID.Equals("upsert"), ).Create( // set these fields if document doesn't exist already db.Post.Published.Set(true), db.Post.Title.Set("title"), db.Post.ID.Set("upsert"), ).Update( // update these fields if document already exists ...
🌐
Prisma Client Python
prisma-client-py.readthedocs.io › en › v0.1.0 › reference › operations
Query Operations - Prisma Client Python - Read the Docs
post = await client.post.upsert( where={ 'id': 'cksc9ld4z0007f08z7obo806s', }, data={ 'create': { 'title': 'This post was created!', 'published': False, } 'update': { 'title': 'This post was updated', 'published': True, }, }, include={ 'categories': True, } )
🌐
GitHub
github.com › prisma › prisma › discussions › 17974
Upsert into SQL table by multiple conditions failure in prisma python? #702 · prisma/prisma · Discussion #17974
How should I query over relation ... value3 (PK) My written code: record = db.table.upsert( where={'column1': 1, 'column2': 2, 'column3': 3} data={ 'create': { .... }, 'update': { .... }, }, ) And the error that I encounterd ...
Author   prisma
🌐
Daily Dev Tips
daily-dev-tips.com › posts › how-to-perform-non-updating-upserts-in-prisma
Non-updating Upserts in Prisma [2024]
February 5, 2024 - An “upsert” in Prisma is a combination of “update” and “insert”. An upsert allows us to update an existing record if it’s found, or insert a new record if it’s not found, based on a unique identifier, such as the playlist URI in our ...
Find elsewhere
🌐
DEV Community
dev.to › dailydevtips1 › how-to-perform-non-updating-upserts-in-prisma-4e3a
How to perform non updating upserts in Prisma - DEV Community
October 28, 2021 - const playlist = await prisma.playlist.upsert({ where: { uri: uri, }, update: {}, create: playlistItem, }); And there you go. This gives us a super good way only to create this if it doesn't exist functionality.
🌐
Prisma
prisma.io › home › prisma client api › prisma client api › prisma client api
Prisma Client API | Prisma Documentation
When Prisma Client does an upsert, it first checks whether that record already exists in the database. To make this check, Prisma Client performs a read operation with the where clause from the upsert operation.
🌐
CodeSandbox
codesandbox.io › s › prisma-upsert-reproduction-2tq0t
Codesandbox
This website utilizes cookies to enable essential site functionality and analytics. You may change your settings at any time or accept the default settings. You may close this banner to continue with only essential cookies. Read more about this in our privacy and cookie statement
🌐
CodeSandbox
codesandbox.io › s › prisma-upsert-reproduction-2tq0t
prisma upsert reproduction - CodeSandbox
December 21, 2020 - prisma upsert reproduction by guillaumeLamanda using @prisma/cli, @prisma/client
Published   Dec 09, 2020
Author   guillaumeLamanda
🌐
Medium
medium.com › @rsharma7_be22 › how-i-optimized-bulk-inserts-by-replacing-upsert-with-createmany-in-prisma-676fa74fb479
How I Optimized Bulk Inserts by Replacing .upsert with createMany in Prisma | by rohanS | Medium
July 30, 2025 - Initially, I was iterating over an array of founders and running an upsert for each one: await Promise.all( founders.map((founder) => prisma.userFounderStatus.upsert({ where: { userId_founderId: { userId: user.id, founderId: founder.id, }, }, ...
🌐
Paigeniedringhaus
paigeniedringhaus.com › blog › tips-and-tricks-for-using-the-prisma-orm
Tips and Tricks for Using the Prisma ORM | Paige Niedringhaus
After the model’s defined, when a new event comes in (like the example above), a Prisma upsert() function can be called on the Event table.
🌐
GitHub
github.com › prisma › prisma › discussions › 3432
Distinguishing between update/insert in upsert · prisma/prisma · Discussion #3432
August 27, 2020 - I'd think it would still "touch" the record, e.g. for a model with an @updateAt column, which prisma automatically sets to the current time whenever the record is updated, I'd expect it to get updated in this case. But it does not. Therefore the record was neither "updated" nor "inserted", so "upsert" doesn't quite seem fitting for what just happened.
Author   prisma
🌐
Prisma
prisma.io › home › transactions and batch queries › transactions and batch queries › transactions and batch queries › transactions and batch queries
Transactions and batch queries (Reference) | Prisma Documentation
Idempotency is the ability to run ... one thousand times. For example: NOT IDEMPOTENT: Upsert (update-or-insert) a user in the database with email address "letoya@prisma.io"....