Django doesn't see relation in workflow tests

In normal tests running locally, but if I try to run them through workflows I get an error:

Run chmod +x tests/test.sh
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "tours_data.city" does not exist
LINE 1: ...ntry_id", "tours_data"."city"."point"::bytea FROM "tours_dat...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/work/tours_manager/tours_manager/manage.py", line 22, in <module>
    main()
  File "/home/runner/work/tours_manager/tours_manager/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/contrib/admin/__init__.py", line 50, in autodiscover
    autodiscover_modules("admin", register_to=site)
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/utils/module_loading.py", line 58, in autodiscover_modules
    import_module("%s.%s" % (app_config.name, module_to_search))
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/runner/work/tours_manager/tours_manager/manager/admin.py", line 5, in <module>
    from .forms import AddressForm, ReviewForm
  File "/home/runner/work/tours_manager/tours_manager/manager/forms.py", line 40, in <module>
    class FindToursForm(forms.Form):
  File "/home/runner/work/tours_manager/tours_manager/manager/forms.py", line 43, in FindToursForm
    country_choice_list += [(city.country.id, city.country.name) for city in city_objects]
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/models/query.py", line 394, in __iter__
    self._fetch_all()
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/models/query.py", line 1867, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/models/query.py", line 87, in __iter__
    results = compiler.execute_sql(
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1398, in execute_sql
    cursor.execute(sql, params)
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "tours_data.city" does not exist
LINE 1: ...ntry_id", "tours_data"."city"."point"::bytea FROM "tours_dat...

Django complains about the following construction inside forms.py:

class FindToursForm(forms.Form):
    country_choice_list, city_choice_list = [('', '')], [('', '')]
    city_objects = City.objects.all()
    country_choice_list += [(city.country.id, city.country.name) for city in city_objects]
    city_choice_list += [(city.id, city.name) for city in city_objects]
    country = forms.ChoiceField(choices=country_choice_list, required=True)
    city = forms.ChoiceField(choices=city_choice_list, required=True)

Namely, on the line country_choice_list += [(city.country.id, city.country.name) for city in city_objects]

pylint.yml:

name: library_app tests
on: [push]
jobs:
  container-job:
    name: Tests
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgis/postgis
        env:
          POSTGRES_PASSWORD: test
          POSTGRES_DBNAME: postgres
          POSTGRES_USER: test
          POSTGRES_PORT: 5432
          POSTGRES_HOST: 127.0.0.1
        ports:
        - 5432:5432
    steps:
    - uses: actions/checkout@v2
    - name: Install Python
      uses: actions/setup-python@v2
      with:
        python-version: "3.10.12"
    - name: Install modules and dependencies
      run: |
        sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
        sudo apt-get update
        sudo apt-get install gdal-bin libgdal-dev
        pip install GDAL==3.6.4
        python -m pip install --upgrade pip
        pip install -r tests/requirements.txt
    - name: API tests
      run: |
        chmod +x tests/test.sh
        ./tests/test.sh tests.test_api

runner.py:

"""Database setup."""

from types import MethodType
from typing import Any

from django.db import connections
from django.db.backends.base.base import BaseDatabaseWrapper
from django.test.runner import DiscoverRunner


def prepare_db(self):
    """Create database schema.

    Args:
        self: type - class object.

    """
    self.connect()
    self.connection.cursor().execute('CREATE SCHEMA IF NOT EXISTS tours_data;')


class PostgresSchemaRunner(DiscoverRunner):
    """Run table schema."""

    def setup_databases(self, **kwargs: Any) -> list[tuple[BaseDatabaseWrapper, str, bool]]:
        """Pre-setup database for work.

        Args:
            kwargs: Any - key word arguments.

        Returns:
            list[tuple[BaseDatabaseWrapper, str, bool]]: setupped database.
        """
        for conn_name in connections:
            connection = connections[conn_name]
            connection.prepare_database = MethodType(prepare_db, connection)
        return super().setup_databases(**kwargs)

Yes, it is 100% listed in settins.py

tests.sh:

export DJANGO_SECRET_KEY="django-insecure-pt4!7*d%-dn)&lx6@598y(1!!rljgaieunnwk!n)-#r^ew(dcb"
export PG_HOST=127.0.0.1
export PG_PORT=5432
export PG_USER=test
export PG_PASSWORD=test
export PG_DBNAME=postgres
export MINIO_ACCESS_KEY_ID=ULk99g9UwyucWln57ypo
export MINIO_SECRET_ACCESS_KEY=dGKHOvOO9166VEMOO0tSRdUVVUcm5odgYgB2Yfjs
export MINIO_STORAGE_BUCKET_NAME=static
export MINIO_API=http://localhost:9000
export MINIO_CONSISTENCY_CHECK_ON_START=False


python3 manage.py test --verbosity=3 $1

If you need more code let me know.

All the code is here: https://github.com/rTiRe/tours_manager Folder with initialized django app: manager

I'm guessing the error is that the tests initially check all my code, and it accesses tables that have not yet been created, but I have no idea how to fix it :(

Sorry for my English, I still use a translator quite often.

Back to Top