Django Ошибка Psycopg2 при запуске миграций в тестировании - PullRequest
0 голосов
/ 16 апреля 2020

Я разработал базовую c версию приложения Django в SQLite3. Затем пришло время переключить его на Postgres и запустить в производство. При этом я заметил, что смог создать и запустить миграции для новой базы данных.

Однако при попытке запустить тесты для версии Postgres я столкнулся с проблемой. Вот оно.

Я запускаю тесты с coverage run --source='.' manage.py test; однако, то же самое происходит при запуске через python manage.py test

Creating test database for alias 'default'...
Got an error creating the test database: database "test_app_web" already exists

Type 'yes' if you would like to try deleting the test database 'test_app_web', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
Creating test database for alias 'extra'...
Traceback (most recent call last):
  File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column aldryn_people_person.name does not exist
LINE 1: SELECT "aldryn_people_person"."id", "aldryn_people_person"."...

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

Traceback (most recent call last):
File "manage.py", line 20, in <module>
    execute_from_command_line(sys.argv)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/commands/test.py", line 56, in handle
    failures = test_runner.run_tests(test_labels)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/test/runner.py", line 604, in run_tests
    old_config = self.setup_databases()
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/test/runner.py", line 551, in setup_databases
    self.parallel, **kwargs
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/test/utils.py", line 174, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 68, in create_test_db
    run_syncdb=True,
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/aldryn_people/migrations/0007_copy_group.py", line 12, in fk_to_m2m
    for person in Person.objects.all():
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/models/query.py", line 268, in __iter__
    self._fetch_all()
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/models/query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
    cursor.execute(sql, params)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
    File "/home/user/miniconda3/envs/app-web-test/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
    django.db.utils.ProgrammingError: column aldryn_people_person.name does not exist
    LINE 1: SELECT "aldryn_people_person"."id", "aldryn_people_person"."...

Рассматриваемое приложение и миграция aldryn_people/migrations/0007_copy_group.py, которая выполняется, когда появляется ошибка, находится здесь: https://github.com/divio/aldryn-people/blob/master/aldryn_people/migrations/0007_copy_group.py

Метод, который, кажется, выдает ошибку, следующий:

def fk_to_m2m(apps, schema_editor):
    """
    Migrates any existing `group` to be the new, m2m `groups`
    """
    Person = apps.get_model("aldryn_people", "Person")
    for person in Person.objects.all():
        if person.group:
            person.groups.add(person.group)
            person.save()

Далее, я заметил, что здесь была обнаружена похожая проблема: Django отношение ошибка при выполнении make migrations

Тем не менее, я не уверен, действительно ли это похоже или просто так.

Любая помощь по устранению неполадок приветствуется.

...