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 users = await prisma.user.updateManyAndReturn({ where: { email: { contains: "prisma.io" } }, data: { role: "ADMIN" }, }); 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" }, }); To emulate findOrCreate(), use upsert() with an empty update parameter.
Discussions

how do I do this upsert query in prisma?
PrismaJoinThe official Discord server of Prisma! More on answeroverflow.com
🌐 answeroverflow.com
August 28, 2024
Use `INSERT ... ON CONFLICT DO NOTHING` instead of unexpected SELECT + INSERT on calling `upsert` with empty `update`
Bug description TLDR: When calling upsert with an empty update object prisma does a SELECT + INSERT instead of a INSERT ... ON CONFLICT DO NOTHING. More context here: prisma/web#5048 (comment) How ... More on github.com
🌐 github.com
6
July 14, 2023
how to upsert many fields in prisma ORM
How can I upsert many fields in prisma ORM with one query? I don't want to use upsert fields one by one. Can I upsert all of them with one query? More on stackoverflow.com
🌐 stackoverflow.com
Provide `upsertMany()`/`upsertFirst()` operation in the client API
Problem I'm frustrated when I want to do an upsert of a specific row, but I don't have fields that are in WhereUniqueInput - I only have fields that, according to my business l... More on github.com
🌐 github.com
70
November 2, 2020
🌐
GitHub
github.com › prisma › prisma › discussions › 22501
Upsert with a nested to-many set · prisma/prisma · Discussion #22501
In the update -> set, I should be able to identify the list via authorId_title, since I don't know the listId, or even better, not at all since it's nested within the list's own upsert. Am I missing anything or is this not possible at the moment? ... Beta Was this translation helpful? Give feedback. ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... Thank you for raising this question. Prisma Client does not currently support upsert operations on related entries with composite IDs in a single query when the ID is not known.
Author   prisma
🌐
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, }, }, update: {}, create: { userId: user.id, founderId: founder.id, isSent: false, }, }) ) );
🌐
Goprisma
goprisma.org › docs › walkthrough › upsert
Upsert records – Prisma Client Go
October 22, 2024 - Prisma Client Go Blog · Light · DocumentationWalkthroughUpsert records · Use upsert to update or create records depending on whether it already exists or not.
Find elsewhere
🌐
Prisma
prisma.io › home › relation queries › relation queries › relation queries › relation queries
Relation queries (Concepts) | Prisma Documentation
const result = await prisma.user.update({ where: { id: 6, }, data: { posts: { update: { where: { id: 9, }, data: { title: "My updated title", }, }, }, }, include: { posts: true, }, }); The following query uses a nested upsert to update "bob@prisma.io" if that user exists, or create the user if they do not exist: const result = await prisma.post.update({ where: { id: 6, }, data: { author: { upsert: { create: { email: "bob@prisma.io", name: "Bob the New User", }, update: { email: "bob@prisma.io", name: "Bob the existing user", }, }, }, }, include: { author: true, }, }); You can nest create or createMany inside an update to add new related records to an existing record.
🌐
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
🌐
GitHub
github.com › prisma › prisma › issues › 20229
Use `INSERT ... ON CONFLICT DO NOTHING` instead of unexpected SELECT + INSERT on calling `upsert` with empty `update` · Issue #20229 · prisma/prisma
July 14, 2023 - Bug description TLDR: When calling upsert with an empty update object prisma does a SELECT + INSERT instead of a INSERT ... ON CONFLICT DO NOTHING. More context here: prisma/web#5048 (comment) How ...
Author   prisma
🌐
Paigeniedringhaus
paigeniedringhaus.com › blog › tips-and-tricks-for-using-the-prisma-orm
Tips and Tricks for Using the Prisma ORM | Paige Niedringhaus
Because of the project requirements, which I’ll get into shortly, we ended up doing some pretty interesting things with Prisma including upsert transactions, querying for the most recent record in a table, fetching related data from multiple tables in one query, handling raw JSON data, and more.
🌐
Prisma
prisma.io › home › prisma client api › prisma client api › prisma client api
Prisma Client API | Prisma Documentation
If multiple upsert operations happen at the same time and the record doesn't already exist, then one or more of the operations might return a unique key constraint error. When Prisma Client does an upsert, it first checks whether that record already exists in the database.
🌐
GitHub
github.com › prisma › prisma › issues › 4134
Provide `upsertMany()`/`upsertFirst()` operation in the client API · Issue #4134 · prisma/prisma
November 2, 2020 - Problem I'm frustrated when I want to do an upsert of a specific row, but I don't have fields that are in WhereUniqueInput - I only have fields that, according to my business logic, will find a unique row. Currently, I have to do ...
Author   prisma
🌐
Reddit
reddit.com › r/nextjs › prisma - is a nested upsert something that makes sense?
r/nextjs on Reddit: Prisma - is a nested upsert something that makes sense?
June 17, 2024 -

I have a question about nested upserts and if they're even possible.

I have a Prisma create that's working as expected;

An orders (table) can create an array of artwork (table), and the artwork can create an array of shirts (table).

The issue is when I want to update the shirts table through the order, similar to the create, with an upsert.

This block works as expected.

const CREATE = await prisma.orders.create({
    data: {
        artwork: {
            create: artwork.map(art => {
                return {
                    notes: art.notes,
                    shirts: {
                        create: art.shirts.map(shirt => ({
                            name: shirt.name,
                            url: shirt.url,
                        })),
                    },
                };
            })
        }
    },
});

This block works to upsert the artwork, but fails when trying to upsert the shirts.

const UPDATE = await prisma.orders.create({
    data: {
        artwork: {
            upsert: artwork.map(art => {

                const updatedArtworkWithShirts = {
                    notes: art.notes,
                    shirts: {
                        upsert: art.shirts.map(shirt => {
                            
                            const updatedShirt = {
                                name: shirt.name,
                                url: shirt.url,
                            };

                            return {
                                where: { id: shirt.id },
                                update: updatedShirt,
                                create: updatedShirt,
                            };
                        }),
                    },
                };

                return {
                    where: { id: art.id },
                    update: updatedArtworkWithShirts,
                    create: updatedArtworkWithShirts,
                };
            }),
        },
    }
});

The error I'm receiving is:

Unknown arg `upsert` in data.artwork.upsert.0.create.shirts.upsert for type shirtsCreateNestedManyWithoutArtworkInput. Did you mean `select`? 

Any thoughts? Am I approaching this incorrectly?

🌐
Brendonovich
prisma.brendonovich.dev › writing-data › upsert
Upserting – Prisma Client Rust
Upserting allows you to update a record if it exists, or create it if it does not. ... model Post { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt published Boolean title String content String? views Int @default(0) } The following example searches ...
🌐
GitHub
github.com › prisma › prisma › issues › 5233
`upsert()` `where` is defined as `number | undefined` but does not allow `undefined` to not select anything · Issue #5233 · prisma/prisma
January 21, 2021 - prisma / prisma Public · Notifications · You must be signed in to change notification settings · Fork 2.3k · Star 46.3k · New issueCopy link · New issueCopy link · Open · Open · upsert() where is defined as number | undefined but does not allow undefined to not select anything#5233 ·
Author   prisma
🌐
Prisma
prisma.io › dataguide › postgresql › inserting-and-modifying-data
Inserting and modifying data in PostgreSQL
How to use `INSERT ON CONFLICT` to upsert data in PostgreSQL · Importing and exporting data in PostgreSQL · Understanding and using transactions in PostgreSQL · Create a managed Postgres database · Hosted Postgres with automated backups, connection pooling, and Query Insights — ready in seconds. Create a databaseExplore Prisma Postgres ·