Invalid column name error after running makemigrations

Hi, I’m seeing an error when running makemigrations after adding a field to an already migrated model.

The models have been fully migrated before without issue, but when I try to add the page_group field in the Page model (the only change I’m making), I get the following error:

django.db.utils.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'PageGroup'. (207) (SQLExecDirectW)")

For reference, I am using django-mssql as my SQL backend.

models.py

from django.core.validators import RegexValidator
from django.db import models


class Page(models.Model):
    id = models.BigAutoField(
        primary_key=True,
        db_column='PageID'
    )
    page_group = models.CharField(
        max_length=100,
        db_column='PageGroup',
        default='Project Reporting'
    )
    page_name = models.CharField(
        max_length=100,
        db_column='PageName'
    )
    page_url = models.CharField(
        max_length=100,
        db_column='PageUrl'
    )
    page_order_id = models.IntegerField(
        db_column='PageOrderID'
    )

    def __str__(self):
        return self.page_name


class Selection(models.Model):
    id = models.BigAutoField(
        primary_key=True,
        db_column='SelectionID'
    )
    contract_id = models.CharField(
        max_length=10,
        db_column='ContractID',
        validators=[
            RegexValidator(
                regex='^[A-Z][0-9]{3}$',
                message='ContractID not of correct format: ^[A-Z][0-9]{3}$ required: e.g. L024, D481. Case sensitive.'
            )
        ]
    )
    page = models.ManyToManyField(
        Page,
        db_column='SelectedPage',
        through='RecordedSelections',
        verbose_name='Select Pages'
    )
    is_latest_selection = models.BooleanField(
        default=True,
        db_column='IsLatestSelection'
    )
    selection_date = models.DateTimeField(
        db_column='SelectionDateTime',
        auto_now=True
    )


class RecordedSelections(models.Model):
    id = models.BigAutoField(
        primary_key=True,
        db_column='RecordedSelectionID'
    )
    selection_id = models.ForeignKey(
        Selection,
        db_column="SelectionID",
        on_delete=models.CASCADE
    )
    page_id = models.ForeignKey(
        Page,
        db_column="PageID",
        on_delete=models.CASCADE
    )

Traceback:

(venv) PS C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app> python manage.py makemigrations site_display
Traceback (most recent call last):
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\mssql\base.py", line 621, in execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyodbc.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'PageGroup'. (207) (SQLExecDirectW)")

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

Traceback (most recent call last):
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app\manage.py", line 22, in <module>
    main()
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\management\base.py", line 453, in execute
    self.check()
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\management\base.py", line 485, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    return check_method()
           ^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\urls\resolvers.py", line 494, in check
    for pattern in self.url_patterns:
                   ^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\urls\resolvers.py", line 715, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
                       ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\urls\resolvers.py", line 708, in urlconf_module
    return import_module(self.urlconf_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app\web_app\urls.py", line 28, in <module>
    path('site-display/', include("site_display.urls"), name='site-display'),
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\urls\conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app\site_display\urls.py", line 4, in <module>
    from . import views
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app\site_display\views.py", line 5, in <module>
    from .forms import PageSelectionForm
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app\site_display\forms.py", line 5, in <module>
    class PageSelectionForm(forms.ModelForm):
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\web_app\site_display\forms.py", line 7, in PageSelectionForm
    choices=[(page.id, page.page_name) for page in Page.objects.all().order_by('page_order_id')],
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\models\query.py", line 398, in __iter__
    self._fetch_all()
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\models\query.py", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\models\query.py", line 91, in __iter__
    results = compiler.execute_sql(
              ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\models\sql\compiler.py", line 1562, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\james.murkin\OneDrive - GMI Construction\Documents\GitHub\gmi-apps\venv\Lib\site-packages\mssql\base.py", line 621, in execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'PageGroup'. (207) (SQLExecDirectW)")

Any help would be appreciated :slight_smile: