question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When using `relationMode="prisma"` (or MongoDB), provide an escape hatch to remove emulated checks from `onUpdate` and `onDelete`

See original GitHub issue

Problem

We have a preview feature called referentialIntegrity It allows users to set the datasource property relationMode to "prisma" or "foreignKeys" When relationMode="prisma"

  • Prisma Client emulates the behavior of the referential actions, onUpdate, onDelete based on the behavior observed in SQL databases that use Foreign Keys.
  • Prisma Migrate does not create Foreign Keys,

For MongoDB provider, relationMode="prisma" is the default and cannot be changed, it makes it easier to work with relations.

In some cases, people maybe want to have an alternative to this emulated behavior of Prisma Client

  • In MongoDB world this is a new behavior
    • some people might prefer handling relations on their own
      • which I think make sense if the MongoDB database is accessed by another ORM/Client (which does not have the same behavior).
    • some people might find it slower because of additional queries

Suggested solution

Option 1 relationMode="none"

  • Valid for all databases
  • It would be a global setting where all referential actions would be invalid and show a schema validation error.
  • The behavior would be that the default onUpdate, onDelete does “nothing”, no checks would be performed, which means that you could break the Referential Integrity of your data. (The referenced entity might not exist)

Option 2 onUpdate="Nothing" / onDelete="Nothing"

  • Valid for all databases
  • Only valid if relationMode="prisma"
  • It would be a Prisma “referential action” that only exist for Prisma
  • The behavior would be that onUpdate, onDelete does “nothing”, no checks would be performed, which means that you could break the Referential Integrity of your data. (The referenced entity might not exist)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
davesteinbergcommented, Nov 28, 2022

@janpio Yup, I just meant to reaffirm the need and agree that I see the connection to this. Sorry for the noise.

0reactions
Songkeyscommented, Nov 30, 2022

Are you ok with your data getting invalid and you having orphan entries after? What is your motivation not to prevent such actions or removing the invalid valid after? (Just trying to understand)

For example, I have user and post tables. A user will post many posts. But there might be some case where a post is coming to the database while its owner hasn’t been inserted into. Because the data was scrape from the internet, blockchain, random other places. I have no control or idea what data would come or lose first actually. So when inserting, updating, deleting, I need to only update the row itself, not to influence other tables. But when query, they do have relations, which needs the usage of relationMode=“prisma”.

I think this scene is pretty common? Orphan entries are totally acceptable. ORM doesn’t need to assume that we must update or delete the related table’s row if one row is changed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can i perform Cascade in mongoDb? If i delete parent the ...
// I wrote this middleware but I do not know that it is correct and how to use it in the delete topic...
Read more >
Cascade style delete in Mongoose - mongodb - Stack Overflow
This is one of the primary use cases of Mongoose's 'remove' middleware. clientSchema.pre('remove', function(next) { // 'this' is the client being removed.
Read more >
Good explanation of cascade (ON DELETE/UPDATE) behavior
ON DELETE NO ACTION (the default): there is no referential delete action; the referential constraint only specifies a constraint check.
Read more >
Getting Started with MongoDB tutorial: Delete Data ... - YouTube
MongoDB Tutorial: Learn how to use different MongoDB delete functions, as well as MongoDB matching operators. Part of a MongoDB CRUD series.
Read more >
How To Create, Retrieve, Update, and Delete Records in ...
Introduction. MongoDB is a free and open-source NoSQL document database used commonly in modern web applications. In this short tutorial, you'll ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found