Я получаю ошибку "Нет такого столбца" только при выполнении тестов

Возможно, это упускает некоторые ключевые детали и показывает мою относительную новизну в django, но я попытался изолировать свою проблему до наименьшего количества шагов, чтобы воспроизвести проблему.

Состояние запуска: Приложение работает нормально, все тесты пройдены.

  1. Добавьте один столбец с нулевым значением в существующую модель.
  2. makemigrations
  3. migrate

Новое состояние: Приложение работает нормально, запуск тестов приводит к ошибке.

Creating test database for alias 'default'...
Found 23 test(s).
Installed 5 object(s) from 1 fixture(s)
Traceback (most recent call last):
  File "/home/f/.virtualenvs/mgmt/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/f/.virtualenvs/mgmt/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 357, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: recommended_for_text

recommend_for_text - это имя нового столбца.

recommended_for_text = models.CharField(null=True, blank=True, max_length=256)

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

Your 0015_migration is very problematic. As a rule of thumb you shouldn't be calling the loaddata command from within a migration since it will use the current model rather than the historical model, this means that a migration might work when you just add it but might fail in the future when more changes are made to the model.

For now you can do the follows:

In 0015_migration replace load_plans in the arguments to RunPython with RunPython.noop:

from django.db import migrations


class Migration(migrations.Migration):
    dependencies = [
        ("sites", "0014_sitecommandlog_note"),
    ]

    operations = [
        migrations.RunPython(migrations.RunPython.noop),
    ]

Next if you really want to use the loaddata command do it externally from the migrations system. If you actually want to load data via migrations consider making data migrations that don't depend on the loaddata command. If you want to load fixtures with data migrations you can emulate what loaddata does by following the guidance in this question: Loading initial data with Django 1.7+ and data migrations

Вернуться на верх