I’m trying to find the answer which would lead to the solution to a problem I’m facing using GraphQLDataFactory:
- I have a “users” and “events” table
- The “events” table has a “userId” column and a constraint with the “users” table:
- I can’t delete a record in “users” without first deleting the corresponding records in the “events” table.
- I can’t enable
ON DELETE CASCADE
.
- I have a factory called
createUser
to create records in the “users” table. - I have another factory called
createUserEvent
that receives a user’s ID and a bunch of other data to create a record in the “events” table. - In a test, I create a user and an event
- When completing the tests, GraphQLDataFactory runs the revert function for each factory in the same order: this causes a problem because I can’t delete the user without deleting the event first.
There are several solutions I can think of, but none is feasible:
- As I wrote, I can’t enable
ON DELETE CASCADE
. - I could use a single mutation that creates both the user and the events, then another that deletes users and events in the proper order. However, that looks more like a hack to me, and it would generate a bunch of factories that are too specialised.
A better solution would be to instruct GraphQLDataFactory to run the revert functions in the correct order.
I’m pretty sure I’m not the first and only person with this problem and that the answer might be somewhere out there, but I couldn’t find it yet.
Any suggestion is more than welcome.