Jest tests for api do not work when prisma schema has non-nullable relations
See original GitHub issueCurrently, 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:
- Created 3 years ago
- Comments:10 (9 by maintainers)

Top Related StackOverflow Question
@jtoar yep this should be fixed, we now delete in a way that works for foreign key constraints!
There have been some updates to Prisma’s CLI. Looks like
migrate resetis getting close to a solution: https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-reset