Django Psycopg2 Error When Running Migrations in Testing

痞子三分冷 提交于 2021-02-11 12:31:27

问题


I developed a basic version of a Django app in SQLite3. Then, it came time to switch it to Postgres and put in production. When doing so, I noticed that I was able to create and run migrations for the new database.

However, when trying to run tests for the Postgres version, I run into an issue. Here it is.

I run tests with coverage run --source='.' manage.py test; however, the same thing happens when running via 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"."...

The app in question and the migration aldryn_people/migrations/0007_copy_group.py that is in process when the error appears is located here: https://github.com/divio/aldryn-people/blob/master/aldryn_people/migrations/0007_copy_group.py

The method which seems to be throwing the error is the following:

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()

Further, I noticed that there was a similar issue reported here: Django relation error when running make migrations

However, I am unsure as to whether it is actually similar or just seems that way.

Any help on troubleshooting this is appreciated.

来源:https://stackoverflow.com/questions/61245569/django-psycopg2-error-when-running-migrations-in-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!