1

I have a functional Django project with a functional database. When I try to run tests on this project, I get this error during database creation:

django.db.utils.InternalError: (1553, "Cannot drop index 'One response per question per level': needed in a foreign key constraint")

I had a unique_together constraint in one of the tables which I later removed.

This line

'One response per question per level'

is related to that unique_together constraint and is present in two migration files - first time during the creation of the table and second time during the removal.

When I run the tests, it is throwing this error and the database is not getting created. How can I solve this problem?

Outsider
  • 1,166
  • 1
  • 14
  • 27
  • Did the second migration when you removed the unique together constraint actually work for your local or development and/or production environment? – Mehran Apr 02 '20 at 13:36
  • @Mehran I don't really remember. It was at least 6 months ago. Is it possible to remove this code from the migration files and make some corresponding changes in the database to reflect these changes? – Outsider Apr 02 '20 at 13:49
  • I don't think that's a good idea tbh, django migrations are very picky and might need a full database reboot if things get messed up. You can however check if the migration in question got applied or not from the django_migrations table in your database (you'll need to use a db client). Check if the migration in question is inside that table. – Mehran Apr 02 '20 at 13:55
  • @Mehran Thanks for the tip. Yes, the migration is applied. – Outsider Apr 02 '20 at 14:00
  • in that case I believe the issue is not database related but on how you're writing your test code, especially when you're trying to ```setUp``` the test case. – Mehran Apr 02 '20 at 14:02
  • @Mehran I removed the setUp() code and yet it is showing the same error. What more can I share with you to make it easier for you to detect the problem? – Outsider Apr 02 '20 at 14:15
  • Are the databases you use in your production and local/dev environment different? e.g you use postgres in production but use sqlite in local? – Mehran Apr 02 '20 at 14:38
  • @Mehran No, I'm using mysql everywhere. – Outsider Apr 02 '20 at 14:59
  • 1
    I'm sorry but it seems this is a Django issue https://code.djangoproject.com/ticket/24757 – Mehran Apr 03 '20 at 12:23
  • @Mehran I see. Thank you very much for your help. – Outsider Apr 04 '20 at 06:14

1 Answers1

0

This seems like a known problem in Mysql code.djangoproject.com/ticket/24757

I'm using this workaround for now.

Outsider
  • 1,166
  • 1
  • 14
  • 27