"no such column" after adding a field to the model

Well the problem is NOT with your makemigrations command or models.py. It is because you have probably imported your class in the model (your database in this case) in on of your views.py files and the problem is with that. If you read the all of the error message then you can understand that easily.

Just try commenting that importing part and run your python.manage.py makemigrations and python manage.py migrate commands then you can uncomment your import in your views.py file

Hope this was useful for others as well


This can happen if you are referencing your model at the root level of your app

This happened to me when I was updating the app mapping_master. I was adding a new field like so:

class MappingMaster(models.Model):

    ...

    # New field that was being added
    statement = models.CharField(max_length=20, choices=STATEMENT_CHOICES, default='PNL', blank=True, null=True)

Gave me the following stacktrace:

D:\Adwaith\codebase\unitapp>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
312, in execute
    django.setup()

....
....

  File "C:\Python27\lib\site-packages\django\apps\config.py", line 198, in impor
t_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "D:\Adwaith\codebase\unitapp\trial_balance_entry\models.py", line 5, in <
module>
    from unitapp import docclass
  File "D:\Adwaith\codebase\unitapp\unitapp\docclass.py", line 139, in <module>
    sample_train_type(type_classifier)
  File "D:\Adwaith\codebase\unitapp\unitapp\docclass.py", line 6, in sample_trai
n_type
    for mapping in MappingMaster.objects.all():

....
....

  File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
318, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: mapping_master_mappingmaster.statement

Turns out that my problem was in another file entirely. It was in the trial_balance_entry app:

...
# The important line is below
from unitapp import docclass


class TrialBalanceEntry(models.Model):
    ...

And inside docclass.py, I had:

import re, csv, os
from mapping_master.models import MappingMaster


def sample_train_type(cl):
    for mapping in MappingMaster.objects.all():
        cl.train(mapping.entry, mapping.type)


def sample_train_category(cl):
    for mapping in MappingMaster.objects.all():
        cl.train(mapping.entry, mapping.category)

...

Turns out that the MappingMaster model instances were being referenced at the root of the app (since I imported it at the start of the file in the models file in trial_balance_entry.

I fixed that by moving the import to one of the inner methods of my TrialBalanceEntry model. This made sure I didn't have any hidden circular dependencies.

P.S. From next time, please provide a stacktrace from the console so we can debug it more easily.


Fast fix.

First remove field scores, Then

python manage.py makemigrations
python manage.py migrate

If any error happens

python manage.py migrate --fake

Then add field scores.Then

python manage.py makemigrations
python manage.py migrate

Hope this helps.

Tags:

Python

Django