Hi @TheLexoPlexx 👋

The second version is creating a new Abwesenheit object using the create method, and then connecting it to an existing Zeitraum object using the connect method. it guarantees that only one Abwesenheit object is associated with a given Zeitraum object, as required by the relation between the two models. However in the first version, you are using the create method to create a new Abwesenheit object and associate it with an existing Zeitraum object, using the abwesenheit field. However, this approach can cause a conflict if the Zeitraum object already has an associated Abwesenheit object.

🌐
Prisma
prisma.io › home › crud › crud › crud › crud
CRUD (Reference) | Prisma Documentation
CreateCreate a single recordWhere ... a single recordUpdate multiple recordsUpdate and return multiple recordsUpsert (update or create)Atomic ......
Discussions

Create or update many
Hi all, I have a scenario where I am making external API calls, passing the response and storing it in my database with prisma. The IDs (PKs) which I am using match that on the external API. The ID... More on github.com
🌐 github.com
2
1
December 5, 2022
Create if not exists
What if I just want to create rows that doesn't exists without updating existing one? I can do it by checking in a loop, but is there any way to do it without loop? 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. ... return await prisma... More on github.com
🌐 github.com
3
7
February 24, 2021
I need help with create, update and find prisma
Post your schema and I’ll be able to help. More on reddit.com
🌐 r/graphql
1
0
July 14, 2024
Update item and create relations if exists, otherwise skip
At the moment, an upsert with an empty create clause would throw an error. The most straightforward solution, I think would be to do a check beforehand to validate if a user exists (With a find unique query) and then, depending on the result, either invoke an update query or do nothing in case ... More on github.com
🌐 github.com
1
8
Top answer
1 of 2
14

I'm providing my solution based on the clarifications you provided in the comments. First I would make the following changes to your Schema.

Changing the schema

model A_User {
  id        Int          @id
  username  String
  age       Int
  bio       String       @db.VarChar(1000)
  createdOn DateTime     @default(now())
  features  A_Features[]
}

model A_Features {
  id          Int      @id @default(autoincrement())
  description String   @unique
  users       A_User[]
}

Notably, the relationship between A_User and A_Features is now many-to-many. So a single A_Features record can be connected to many A_User records (as well as the opposite).

Additionally, A_Features.description is now unique, so it's possible to uniquely search for a certain feature using just it's description.

You can read the Prisma Guide on Relations to learn more about many-to-many relations.

Writing the update query

Again, based on the clarification you provided in the comments, the update operation will do the following:

  • Overwrite existing features in a A_User record. So any previous features will be disconnected and replaced with the newly provided ones. Note that the previous features will not be deleted from A_Features table, but they will simply be disconnected from the A_User.features relation.

  • Create the newly provided features that do not yet exist in the A_Features table, and Connect the provided features that already exist in the A_Features table.

You can perform this operation using two separate update queries. The first update will Disconnect all previously connected features for the provided A_User. The second query will Connect or Create the newly provided features in the A_Features table. Finally, you can use the transactions API to ensure that both operations happen in order and together. The transactions API will ensure that if there is an error in any one of the two updates, then both will fail and be rolled back by the database.


//inside async function
const disconnectPreviouslyConnectedFeatures =  prisma.a_User.update({
    where: {id: 1},
    data: {
        features: {
            set: []  // disconnecting all previous features
        }
    }
})

const connectOrCreateNewFeatures =  prisma.a_User.update({
    where: {id: 1},
    data: {
        features: {
            // connect or create the new features
            connectOrCreate: [
                {
                    where: {
                        description: "'first feature'"
                    }, create: {
                        description: "'first feature'"
                    }
                },
                {
                    where: {
                        description: "second feature"
                    }, create: {
                        description: "second feature"
                    }
                }
            ]
        }
    }
})

// transaction to ensure either BOTH operations happen or NONE of them happen.
await prisma.$transaction([disconnectPreviouslyConnectedFeatures, connectOrCreateNewFeatures ])

If you want a better idea of how connect, disconnect and connectOrCreate works, read the Nested Writes section of the Prisma Relation queries article in the docs.

2 of 2
2

The TypeScript definitions of prisma.a_User.update can tell you exactly what options it takes. That will tell you why the 'features' does not exist in type error is occurring. I imagine the object you're passing to data takes a different set of options than you are specifying; if you can inspect the TypeScript types, Prisma will tell you exactly what options are available.

If you're trying to add new features, and update specific ones, you would need to specify how Prisma can find an old feature (if it exists) to update that one. Upsert won't work in the way that you're currently using it; you need to provide some kind of identifier to the upsert call in order to figure out if the feature you're adding already exists.

https://www.prisma.io/docs/reference/api-reference/prisma-client-reference/#upsert

You need at least create (what data to pass if the feature does NOT exist), update (what data to pass if the feature DOES exist), and where (how Prisma can find the feature that you want to update or create.)

You also need to call upsert multiple times; one for each feature you're looking to update or create. You can batch the calls together with Promise.all in that case.

const upsertFeature1Promise = prisma.a_User.update({
  data: {
    // upsert call goes here, with "create", "update", and "where"
  }
});
const upsertFeature2Promise = prisma.a_User.update({
  data: {
    // upsert call goes here, with "create", "update", and "where"
  }
});
const [results1, results2] = await Promise.all([
  upsertFeaturePromise1,
  upsertFeaturePromise2
]);
🌐
Prisma
prisma.io › home › relation queries › relation queries › relation queries › relation queries
Relation queries (Concepts) | Prisma Documentation
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:
🌐
Prisma
prisma.io › home › prisma client api › prisma client api › prisma client api
Prisma Client API | Prisma Documentation
To make this check, Prisma Client ... possible outcomes, as follows: If the record does not exist, then Prisma Client creates that record. If the record exists, then Prisma Client updates it....
🌐
Prisma
v1.prisma.io › docs › 1.34 › prisma-client › basic-data-access › writing-data-JAVASCRIPT-rsc6
Prisma 1.34 - Writing Data (JavaScript) with JavaScript
create: Creates a new Post record and connects it via the author field to the User record. update: Updates a Post record that is connected to a User record via the author field. upsert: Updates a Post record that is connected to a User record ...
Find elsewhere
🌐
Medium
medium.com › @GauravBR › beginners-guide-to-create-read-update-delete-data-using-prisma-f2b78db3a2ff
Beginners guide to Create, Read, Update & Delete Data using Prisma | by Gaurav Radadiya | Medium
December 12, 2023 - This guide covers basic details on how to fetch, update, delete or add data in database using Prisma. prisma.<tablename>.<command> tablename = prisma models commands = create, findUnique, findMany, update, updateMany, delete, deleteMany
🌐
SabinTheDev
sabinadams.hashnode.dev › basic-crud-operations-in-prisma
Prisma and TypeScript CRUD Basics - Sabin Adams
June 21, 2024 - Prisma has a bunch of cool features that allow us to update our data in various ways. We can update one or many record, or even choose to create a record if no matching record was found to update using upsert.
🌐
Paigeniedringhaus
paigeniedringhaus.com › blog › tips-and-tricks-for-using-the-prisma-orm
Tips and Tricks for Using the Prisma ORM | Paige Niedringhaus
According to Prisma’s docs, the connectOrCreate API function is the way to create a “related record” that may or may not exist, which is perfect for adding new devices to the Device table and new, related fleets to the Fleet table at the same time via a create() with nested write function. However, when attempting to update a device’s fleets when a new event came in, I encountered a slight problem with the update() function.
🌐
HatchJS
hatchjs.com › home › prisma create or update: a comprehensive guide
Prisma Create or Update: A Comprehensive Guide
December 25, 2023 - Prisma Create or Update is a command-line tool that allows you to create or update Prisma models. Prisma models are the blueprints for your data, and they define the structure of your data and the relationships between your data.
🌐
YouTube
youtube.com › watch
Prisma Tutorial for Beginners #6 - CRUD - Updating Records - YouTube
Connect with me on LinkedIn: https://www.linkedin.com/in/khurram-ali1 ☕ Buy Me A Coffee ➜ https://www.buymeacoffee.com/giraffereactor Github Repo:https://gi...
Published   February 28, 2025
🌐
Reddit
reddit.com › r/graphql › i need help with create, update and find prisma
r/graphql on Reddit: I need help with create, update and find prisma
July 14, 2024 -

So i have a model Post, which have lets say assets as images and general stuff like text, create time etc. But now that post can be in various feeds but still differ on assets. One feed might have same post with 2 images while other feed have only one.

There is a feedpostassets table to for join of postassets and feedpostid.

How to write create, post, update and find for this pertaining to taking care of assets, using prisma ORM, nestJS

🌐
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.
🌐
YouTube
youtube.com › watch
Upsert (Update or Insert) | Express API & Prisma ORM Query Fundamentals Course - YouTube
Upsert (Update or Insert) | Updating RecordsWelcome to this full-stack development course focused on building robust, database-driven applications using Expr...
Published   May 17, 2025
🌐
Prisma
prisma.io › home › getting started with prisma migrate › getting started with prisma migrate › getting started with prisma migrate
Getting started with Prisma Migrate | Prisma Documentation
Review pricing if you're evaluating Prisma Postgres or managed workflows for production use ... Adding to a new projectCreate an initial migrationAdditional migrationsCommitting to versions controlAdding to an existing projectIntrospect to create or update your Prisma schemaCreate a baseline migrationWork around features not supported by Prisma Schema LanguageApply the initial migrationsCommit the migration history and Prisma schemaGoing furtherWhere to go next
🌐
Hashnode
allahisrabb.hashnode.dev › day-56-creating-and-updating-models-on-prisma-mysql-in-adonisjs
Day 56 - Creating and Updating Models on Prisma + MySQL in AdonisJs
October 11, 2024 - This will create a migration file, apply the changes to your database and also creates/updates the Prisma client with the latest schema, so the new or modified fields and models are available for use in your code.
🌐
Medium
medium.com › @enayetflweb › prisma-crud-operations-guide-211a15113e70
Prisma CRUD Operations Guide. This guide will walk you through using… | by Md Enayetur Rahman | Medium
October 26, 2024 - This guide will walk you through using Prisma for common database operations like creating, updating, upserting, and deleting records…