Problems adding database columns

Hello, everyone,
I have a problem with a model.

The model was initially created:

class Location(models.Model):
    creator = models.CharField(null=False, max_length = 250)
    slug = models.SlugField(null=True)
    checked = models.BooleanField(null=True)
    lat = models.FloatField(null=True)
    lng = models.FloatField(null=True)
    name = models.CharField(null=True, max_length = 250)
    location_name = models.CharField(null=True, blank=False, max_length = 250)

There are various other models that use “ForeignKey” and “OneToOneField” to create a reference to “Location”.

For example, if I now remove “location_name” and run makemigrations:
remove_location_name… OK

It’s all ok.

However, if I now try to add a new database column (makemigrations), e.g.:

class Location(models.Model):
    creator = models.CharField(null=False, max_length = 250)
    slug = models.SlugField(null=True)
    checked = models.BooleanField(null=True)
    lat = models.FloatField(null=True)
    lng = models.FloatField(null=True)
    name = models.CharField(null=True, max_length = 250)
    follower = models.IntegerField(null=True)

I get this error:

django.db.utils.OperationalError: no such column: app_location.follower

Why is that? For all other models - everything works as desired, even in the same app.

Search your entire code base to see if you’re referencing that field anywhere in your project. Don’t forget to look for references in things like order_by or in filter functions.

Hi Ken,
there are no references to the new field. Even if I:

hsjfwehjbfwe = models.CharField(null=True, max_length = 250)

add to the model and run makemigrationas, this is not accepted, it feels like i can’t add new fields to this model.

Ok, maybe I’m a little confused here. Are you getting this message in the makemigrations command or when you try to run your project after you makemigrations?

(Edit: Also, it may be of value if you posted the complete traceback message you’re getting. Sometimes the “real” error is hidden in the full message.)

In both situations, as soon as I complete the field in the model.

Therefore:

  1. I complete the model (server is not running)
  2. run makemigrations
  3. error “django.db.utils.OperationalError: no such column: app_location.follower”

or:

  1. I complete the model (server is not running)
  2. run runserver
  3. error “django.db.utils.OperationalError: no such column: app_location.follower”

if I remove it from the code again - everything works, just without the new database field.

MacBook-Pro-von-Rene local_fishing % python3 manage.py makemigrations
Traceback (most recent call last):
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/sqlite3/base.py”, line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: fishing_waters_waters.follower

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

Traceback (most recent call last):
File “manage.py”, line 22, in
main()
File “manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “Library/Python/3.8/lib/python/site-packages/django/core/management/init.py”, line 419, in execute_from_command_line
utility.execute()
File “Library/Python/3.8/lib/python/site-packages/django/core/management/init.py”, line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “Library/Python/3.8/lib/python/site-packages/django/core/management/base.py”, line 354, in run_from_argv
self.execute(*args, **cmd_options)
File “Library/Python/3.8/lib/python/site-packages/django/core/management/base.py”, line 393, in execute
self.check()
File “Library/Python/3.8/lib/python/site-packages/django/core/management/base.py”, line 419, in check
all_issues = checks.run_checks(
File “Library/Python/3.8/lib/python/site-packages/django/core/checks/registry.py”, line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File “Library/Python/3.8/lib/python/site-packages/django/core/checks/urls.py”, line 13, in check_url_config
return check_resolver(resolver)
File “Library/Python/3.8/lib/python/site-packages/django/core/checks/urls.py”, line 23, in check_resolver
return check_method()
File “Library/Python/3.8/lib/python/site-packages/django/urls/resolvers.py”, line 416, in check
for pattern in self.url_patterns:
File “Library/Python/3.8/lib/python/site-packages/django/utils/functional.py”, line 48, in get
res = instance.dict[self.name] = self.func(instance)
File “Library/Python/3.8/lib/python/site-packages/django/urls/resolvers.py”, line 602, in url_patterns
patterns = getattr(self.urlconf_module, “urlpatterns”, self.urlconf_module)
File “Library/Python/3.8/lib/python/site-packages/django/utils/functional.py”, line 48, in get
res = instance.dict[self.name] = self.func(instance)
File “Library/Python/3.8/lib/python/site-packages/django/urls/resolvers.py”, line 595, in urlconf_module
return import_module(self.urlconf_name)
File “/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1014, in _gcd_import
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in _load_unlocked
File “”, line 783, in exec_module
File “”, line 219, in _call_with_frames_removed
File “Documents/Projekt Django/local_fishing/local_fishing/urls.py”, line 11, in
path(‘gewaesser/’, include(‘fishing_waters.urls’)),
File “Library/Python/3.8/lib/python/site-packages/django/urls/conf.py”, line 34, in include
urlconf_module = import_module(urlconf_module)
File “/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1014, in _gcd_import
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in _load_unlocked
File “”, line 783, in exec_module
File “”, line 219, in _call_with_frames_removed
File “Documents/Projekt Django/local_fishing/fishing_waters/urls.py”, line 2, in
from . import views
File “Documents/Projekt Django/local_fishing/fishing_waters/views.py”, line 148, in
class WaterOverviewView(View):
File “Documents/Projekt Django/local_fishing/fishing_waters/views.py”, line 154, in WaterOverviewView
for entry in Waters.objects.all():
File “Library/Python/3.8/lib/python/site-packages/django/db/models/query.py”, line 280, in iter
self._fetch_all()
File “Library/Python/3.8/lib/python/site-packages/django/db/models/query.py”, line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File “Library/Python/3.8/lib/python/site-packages/django/db/models/query.py”, line 51, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File “Library/Python/3.8/lib/python/site-packages/django/db/models/sql/compiler.py”, line 1175, in execute_sql
cursor.execute(sql, params)
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/utils.py”, line 98, in execute
return super().execute(sql, params)
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/utils.py”, line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/utils.py”, line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “Library/Python/3.8/lib/python/site-packages/django/db/utils.py”, line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “Library/Python/3.8/lib/python/site-packages/django/db/backends/sqlite3/base.py”, line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: fishing_waters_waters.follower

I’d want to take a really good look at your fishing_waters/views.py file. Something is trying to execute model code during initialization.

1 Like

Wow - okay, I didn’t expect that at all.

I identified the error, it’s because of this view:

class WaterOverviewView(View):
    template = "fishing_waters/water-overview.html"
    water_name = []
    for entry in Waters.objects.all():
        water_name.append(entry.water_name)
    context = {
        "water_name": water_name,
    }

    def get(self, request):
        return render(request, self.template, self.context )

Thank you Ken!

Addition - this is a view - yes it was stupid to do it like that :smiley: