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.

Jest tests for api do not work when prisma schema has non-nullable relations

See original GitHub issue

Currently, prisma does not support cascading deletes - See https://github.com/prisma/prisma/issues/2810 You can add ON DELETE CASCADE manually, but that runs into https://github.com/prisma/prisma/issues/2057

So, if your model has non nullable relations without cascade, yarn rw test api fails because the setup https://github.com/redwoodjs/redwood/blob/main/packages/core/src/configs/node/jest.setup.js#L13-L17 tries to invoke deleteMany on all models in the order that they’re defined in schema.prisma. If this order tries to delete a parent model first, we get an error like

Invalid `prisma.user.deleteMany()` invocation:

The change you are trying to make would violate the required relation 'ProfileToUser' between the `Profile` and `User` models.

One hacky workaround is to order the models appropriately in schema.prisma, but would love to have a proper solution for this issue. Another option would be to run a raw SQL query to empty the db. I am not sure if prisma has a command to empty the database. If it does, we can directly use that ideally.

This issue may still persist after prisma supports CASCADE because not all users would want to cascade on delete.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
cannikincommented, Dec 13, 2021

@jtoar yep this should be fixed, we now delete in a way that works for foreign key constraints!

1reaction
thedavidpricecommented, Jan 12, 2021

There have been some updates to Prisma’s CLI. Looks like migrate reset is getting close to a solution: https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-reset

The migrate reset command:

  • Drops the database
  • Creates a new database with the same name
  • Applies all migrations
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit testing with Prisma
Learn how to setup and run unit tests with the Prisma client. ... This guide assumes you have the JavaScript testing library Jest...
Read more >
4. Testing your API - GraphQL Nexus
Setting up your test environment. During this tutorial, you'll use the Jest testing framework to test your API. This is not mandatory but...
Read more >
prisma schema nullable | The AI Search Engine You Control
This is your Prisma schema file, // learn more about it in the docs: ... tests for api do not work when prisma...
Read more >
nexus-prisma - npm
Start using nexus-prisma in your project by running `npm i nexus-prisma`. There are 21 other projects in the npm registry using nexus-prisma.
Read more >
How to write tests for Prisma with Docker and Jest
We will need a Node container to run migrations and tests in. We do this in containers so the environment is consistent for...
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