“no such column” after adding a field to the model

前端 未结 7 1561
抹茶落季
抹茶落季 2020-12-28 08:53

environment DJANGO VERSION 1.9 Python 2.7.6

I added a field (scores) to a model class in models.py like this

from django.db import models
from django         


        
7条回答
  •  一生所求
    2020-12-28 09:40

    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 
        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 
        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.

提交回复
热议问题