0

I set up a Django 2 app (and Python 3.6) with a remote MySQL DB (using mysqlclient), with proper permissions.

When I run my unit test, I get the following error: django.db.utils.ProgrammingError: (1146, “Table ‘test_<db>.<db>’ doesn’t exist”).

When I manually open the website at localhost, everything works fine.

Technically I could create a local dev DB for testing, but I would prefer that the test DB is created automatically.

EDIT

It seems like the issue was related to my tables not being managed by Django, see here.

2 Answers 2

1

I believe this is a duplicate.

Django : Table doesn't exist

If the table is missing, make sure you've got the right migration file (python manage.py makemigrations) and that you've applied it (python manage.py migrate).

3
  • I haven't dropped any migrations and my migrations were done properly. When I manually open my website, everything runs perfectly. Mar 12, 2018 at 17:41
  • Is the table that you're trying to access in the test defined in the migration files? The test DB is not (necessarily) the same one you use when running the unit tests. Mar 12, 2018 at 18:20
  • The table is defined in the migration file. Doesn't Django create a test DB automatically using the DB Engine config? Mar 12, 2018 at 18:43
1

What worked for me:

Changing my tables to managed = True.

Deleting migration files (except __init__.py).

python manage.py makemigrations

python manage.py migrate

And now my unit tests are running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.