2

Recently I've migrated a Django project from version 1.9.1 to 3.2.7.

Now I'm trying to write some new tests, and I'm getting this error:

# python manage.py test
Creating test database for alias 'default'...
Got an error creating the test database: database "test_django" already exists

Type 'yes' if you would like to try deleting the test database 'test_django', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
Traceback (most recent call last):
  File "/opt/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "auth_user" does not exist

I understand this is because currently I don't have any 'migrations' directory, as I cloned the git repo and the DB already existed when the Django project was running in version 1.9.1.

I've read:

All of them recommend running migrations, but:

# python manage.py makemigrations 
No changes detected
# python manage.py makemigrations auth
No changes detected in app 'auth'

Again, I think this is because the DB schema already existed before upgrading to 3.2.

I cannot seem to solve the issue running the migrations approach.

Is there another way to solve this issue, or force to generate the migrations even if the DB already exist and is synced (and possible fake them)?

nKn
  • 13,691
  • 9
  • 45
  • 62
  • Is it not an option to reuse the test database? You can do this with `python manage.py test --keepdb` – Brian Destura Oct 12 '21 at 04:14
  • Is `django.contrib.admin` in `INSTALLED_APPS` of the settings.py file that your test uses? – aaron Oct 12 '21 at 10:03
  • @BrianDestura it is an option, but I'd better like to be able to run the tests recreating the DB on each run. If finally I don't see a way, I'll keep the DB. – nKn Oct 13 '21 at 07:38
  • @aaron I assume it is using the project's `settings.py` file (I'm not overwriting anything), so yes, `django.contrib.admin` is included. – nKn Oct 13 '21 at 07:38
  • Unfortunately to recreate the db you would need the migrations. Just curious, when you cloned, why are the migrations not included? – Brian Destura Oct 13 '21 at 08:44
  • @BrianDestura I had this project with all the migrations. However, I removed the whole git project and cloned it again (without the migrations, as there's an entry in `.gitignore`), as the DB already existed, I didn't need to recreate it. Is there a way to recreate the migrations without changing the DB? – nKn Oct 13 '21 at 09:37
  • @nKn You can temporarily change your settings.py file to point to a new or non-existent SQLite file ([docs.djangoproject.com/en/3.2/ref/settings/#databases](https://docs.djangoproject.com/en/3.2/ref/settings/#databases) and then run `makemigrations`. – aaron Oct 13 '21 at 09:48
  • @aaron The problem with this approach is that the Test db is the same than the app's. If I change the DB to a temporary SQLite file, when changing it back to the real DB after running the migrations, the relation won't exist again. I'm getting the same exception because of this. – nKn Oct 13 '21 at 09:54
  • @aaron The relations exist, but on the real DB. If I change the DB to a temporary SQLite, the migrations get created, but 'auth' is an internal Django app and I don't get the migration files stored. If I run `manage.py migrate`, the relations get created. But if I change the DB value in `settings.py` (which is different from the real DB, as Django prepends the 'test_' string to the test DB), the 'auth' relations are not there. If possible, I'd like to delete the test db on each execution. – nKn Oct 13 '21 at 10:29
  • @aaron Say the project's DB is called 'default'. Once I run `manage.py`, Django will create the DB `test_default` for the tests. I could change the project's DB temporarily to be `test_default`, run a `manage.py migrate` and then use `--keepdb`, but I would prefer if Django could create and destroy the test DB each time the test is run. – nKn Oct 13 '21 at 10:51
  • The test databases are destroyed when all the tests have been executed. I still don't understand the problem. – aaron Oct 13 '21 at 10:59
  • The problem is the `auth` app throws an `UndefinedTable` exception because the tables are not created when the `test` command is run. – nKn Oct 13 '21 at 11:02
  • Why not? Are you using an existing `test_default` DB? – aaron Oct 13 '21 at 11:06
  • I tried both using an existing one and a newly created one. When using a new one, the `auth` app tables seem not to get created, thus the error. – nKn Oct 13 '21 at 11:14
  • Can you share a repro project on GitHub? – aaron Oct 13 '21 at 11:19
  • please add the output of python3 manage.py showmigartions command – YosSaL Oct 15 '21 at 17:39

1 Answers1

2

If the error is because of the migrations you can skip the migration errors while running tests by using the following django library

django-test-without-migrations ( pip install django-test-without-migrations)

Install the library and add it in INSTALLED_APPS (settings.py)

Then run, python manage.py test --nomigrations 

refer:https://pypi.org/project/django-test-without-migrations/

Jisson
  • 3,566
  • 8
  • 38
  • 71