pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.32k stars 341 forks source link

django.db.utils.ProgrammingError: relation "applable_modelname" does not exist #986

Open titovanton opened 2 years ago

titovanton commented 2 years ago

Hello! I got the following, when I run pytest:

pytest==6.2.5
pytest-cov==3.0.0
pytest-django==4.5.2
pytest-forked==1.4.0
pytest-xdist==2.5.0
/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py:85: in _execute
    return self.cursor.execute(sql, params)
E   psycopg2.errors.UndefinedTable: relation "applable_modelname" does not exist

The above exception was the direct cause of the following exception:
/usr/local/lib/python3.10/site-packages/pytest_django/fixtures.py:122: in django_db_setup
    db_cfg = setup_databases(
/usr/local/lib/python3.10/site-packages/django/test/utils.py:208: in setup_databases
    connection.creation.create_test_db(
/usr/local/lib/python3.10/site-packages/django/db/backends/base/creation.py:74: in create_test_db
    call_command(
/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py:187: in call_command
    return command.execute(*args, **defaults)
/usr/local/lib/python3.10/site-packages/django/core/management/base.py:417: in execute
    output = self.handle(*args, **options)
/usr/local/lib/python3.10/site-packages/django/core/management/base.py:90: in wrapped
    res = handle_func(*args, **kwargs)
/usr/local/lib/python3.10/site-packages/django/core/management/commands/migrate.py:223: in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
/usr/local/lib/python3.10/site-packages/django/core/management/commands/migrate.py:346: in sync_apps
    with connection.schema_editor() as editor:
/usr/local/lib/python3.10/site-packages/django/db/backends/base/schema.py:124: in __exit__
    self.execute(sql)
/usr/local/lib/python3.10/site-packages/django/db/backends/base/schema.py:151: in execute
    cursor.execute(sql, params)
/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py:67: in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py:76: in _execute_with_wrappers
    return executor(sql, params, many, context)
/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py:80: in _execute
    with self.db.wrap_database_errors:
/usr/local/lib/python3.10/site-packages/django/db/utils.py:90: in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py:85: in _execute
    return self.cursor.execute(sql, params)
E   django.db.utils.ProgrammingError: relation "applable_modelname" does not exist

applable_modelname - that's for example ofcz :)

That happens when I call pytest. When I specify --reuse-db --nomigrations - tests are passed. That makes me think, that the issue is not in my migrations. What do I do wrong and how to fix it? Thanks!

SHxKM commented 1 year ago

I'm suddenly facing this issue as well, did you ever find a solution @titovanton ?

titovanton commented 1 year ago

I'm suddenly facing this issue as well, did you ever find a solution @titovanton ?

nope

AnonSar commented 10 months ago

Hi there, I'm also facing a similar issue. Would be great if a workaround ( if any ) for this issue could be shared here.

Actionb commented 9 months ago

@AnonSar @titovanton @SHxKM Could it be that this issue is due to having multiple test database configs? I had the same problem when trying to run test with these test settings:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        ...
    },
    'sqlite': {
        'ENGINE': 'django.db.backends.sqlite3'
    }
}

Removing the sqlite config fixed the issue and let me run pytest without the --no-migrations flag.

AhmedHathout commented 1 month ago

@AnonSar @titovanton @SHxKM Could it be that this issue is due to having multiple test database configs? I had the same problem when trying to run test with these test settings:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        ...
    },
    'sqlite': {
        'ENGINE': 'django.db.backends.sqlite3'
    }
}

Removing the sqlite config fixed the issue and let me run pytest without the --no-migrations flag.

This actually worked for me too when I tried it a few mins ago. I do not know why it caused that problem yet though. Thank you