Search code examples
djangopostgresqldjango-tests

Django parallel tests not creating database tables


I'm trying to run Django tests in parallel using the following command:

python manage.py test myproject.myapp.tests --parallel=4 --keepdb

However, I get errors like the following:

...
File "/home/daniel/Envs/myproject/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "accounts_user" does not exist
LINE 1: INSERT INTO "accounts_user" ("password", "last_login", "is_s...
                    ^

which means that the tables are not being created in the Postgres test DBs. As required, I have created DBs named test_myproject_1, test_myproject_2 and so on, and upon inspection in pgAdmin, I see the tables are indeed not there. My assumption was that the tables are created automatically when the tests are run.

I don't get such errors when running the tests on a single thread, in other words, when I don't use the --parallel option or when I use --parallel=1, which uses the test_myproject database. Does anyone have any idea as to what the problem could be?


Solution

  • The problem lies within the use of both --parallel and --keepdb, where the alternative test databases won't be migrated to the current state. Running --parallel without --keepdb will force a refresh of the database schemas in the test databases.

    (If you were unable to run without --keepdb because of permission denied to create database you need to check if your database user has the correct permissions.)

    There is an open issue #26822 and a partial patch aiming to fix this, but there hasn't been any activity for a while. Also see the discussion on Django-developers mailing list.