Can't delete user due to foreign key restraint

I was able to use this command to remove a user:

tutor local run lms ./manage.py lms manage_user --remove <user_name> <user_email>

When I tried it with a second user, I got an error:

django.db.utils.IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`openedx`.`course_creators_coursecreator`, CONSTRAINT `course_creators_coursecreator_user_id_e4da548d_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')
Error: Command failed with status 1: docker-compose -f /home/kit/.local/share/tutor/env/local/docker-compose.yml -f /home/kit/.local/share/tutor/env/local/docker-compose.prod.yml -f /home/kit/.local/share/tutor/env/local/docker-compose.tmp.yml --project-name tutor_local run --rm lms ./manage.py lms manage_user --remove username email

What I’ve tried:

  • Restarting the server and re-running the command leads to the same error.

tutor version 14.0.4

From our experience, we couldn’t delete users with active course enrollments. Hopefully, that will solve the problem. It seems that I’ve had to search for the user references in other places, such as access tokens, to get it to release the user.

Also, try deleting the users in both places, in Django and using manage_user. It seems the one might work when the other option wouldn’t.

1 Like

For me when I ran into this error, I just followed what it said, i.e. I deleted the user from coursecreator table first (not sure if thats is the correct name of the table). And then tried to delete it again from User table. (Using Django/Python Shell)

2 Likes

If you have MySQL access then you can run this script to remove problematic user accounts: https://github.com/lpm0073/cookiecutter-openedx-devops/scripts/sql/delete-user.sql

1 Like

Perfect.

For future reference, here’s how I did it:

  1. Get a mysql console
  2. Find the user ID of the user you’re removing: select id, username, email, is_active from auth_user;
  3. [Optional] Find out which row of the course_creators_coursecreator table needs to be deleted: select * from course_creators_coursecreator;
  4. Delete that user: delete from course_creators_coursecreator where user_id = [ID];
  5. [Optional] Verify that this course_creator row has been removed select * from course_creators_coursecreator;
  6. Exit the mysql console
  7. Re-run the original tutor command: tutor local run lms ./manage.py lms manage_user --remove <user_name> <user_email>
1 Like