# Problems adding database columns

**URL:** <https://forum.djangoproject.com/t/problems-adding-database-columns/14716>\
**Category:** Forms & APIs\
**Created:** [July 9, 2022, 11:36am UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716 "2022-07-09T11:36:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReneS29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renes29/32/6365_2.png) [@ReneS29](https://forum.djangoproject.com/u/ReneS29)\
**Post date:** [July 9, 2022, 11:36am UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/1 "2022-07-09T11:36:52Z")

</div>

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

The model was initially created:

```auto
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.:

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 9, 2022, 11:50am UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/2 "2022-07-09T11:50:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ReneS29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renes29/32/6365_2.png) [@ReneS29](https://forum.djangoproject.com/u/ReneS29)\
**Post date:** [July 9, 2022, 12:14pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/3 "2022-07-09T12:14:00Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 9, 2022, 12:18pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/4 "2022-07-09T12:18:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ReneS29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renes29/32/6365_2.png) [@ReneS29](https://forum.djangoproject.com/u/ReneS29)\
**Post date:** [July 9, 2022, 12:23pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/5 "2022-07-09T12:23:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ReneS29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renes29/32/6365_2.png) [@ReneS29](https://forum.djangoproject.com/u/ReneS29)\
**Post date:** [July 9, 2022, 12:28pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/6 "2022-07-09T12:28:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 9, 2022, 1:53pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/7 "2022-07-09T13:53:33Z")

</div>

> [@ReneS29](#):
>
> 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():

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.

---

<div class="post-metadata">

**Author:** ![ReneS29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renes29/32/6365_2.png) [@ReneS29](https://forum.djangoproject.com/u/ReneS29)\
**Post date:** [July 9, 2022, 3:59pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/8 "2022-07-09T15:59:38Z")

</div>

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

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

```auto
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 😃

---

<div class="post-metadata">

**Author:** ![Kadex](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kadex/32/12802_2.png) [@Kadex](https://forum.djangoproject.com/u/Kadex)\
**Post date:** [March 20, 2023, 10:50am UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/9 "2023-03-20T10:50:42Z")

</div>

Hello, I know this topic has already been solved. But I have the same problem now and I also did something similar in my views.py file: iterating through my\_model.objects.all() with a for loop. My question is: why is it a bad idea? 😅 I am starting with Django and I thought that practice was okay. Can anybody tell me a better way to do this so that I don’t get this error in the future?

Edit: I say it because I simply commented out the piece of code that was causing the error, and then run again **python manage.py makemigrations** and everything worked as intended. But what if I had a much bigger project? I don’t think it would be a great idea to be commenting everything out every time I need a new field for a model…

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 20, 2023, 11:15am UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/10 "2023-03-20T11:15:26Z")

</div>

> [@Kadex](#):
>
> iterating through my\_model.objects.all() with a for loop. My question is: why is it a bad idea?

This by itself is not a bad idea.

The problem is when you do it in the wrong place (at the module level) instead of in the right place (within a function.)

Solving a specific problem encountered requires seeing the actual error message and the code involved. If you want to get into more specifics, please create a new topic for this conversation.

---

<div class="post-metadata">

**Author:** ![tulibsalim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tulibsalim/32/12847_2.png) [@tulibsalim](https://forum.djangoproject.com/u/tulibsalim)\
**Post date:** [March 22, 2023, 3:27pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/11 "2023-03-22T15:27:23Z")

</div>

Facing a same problem like when i do it on the wrong place at module level instead of right place then specific problem appeared[,](https://toweroflondontour.com/tower-of-london-tour-tickets.html) Looking for a solution

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 22, 2023, 4:14pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/12 "2023-03-22T16:14:14Z")

</div>

As I wrote above, solving a specific problem that you are encountering requires seeing the actual error message and the code involved.  
If you are having a problem that you would like assistance with, please create a new topic for this conversation. Include the code causing the error in your post.  
When posting code, copy/paste the code into the body of your message, between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

---

<div class="post-metadata">

**Author:** ![HoneyPaptan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/honeypaptan/32/15471_2.png) [@HoneyPaptan](https://forum.djangoproject.com/u/HoneyPaptan)\
**Post date:** [August 20, 2023, 2:58am UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/13 "2023-08-20T02:58:27Z")

</div>

heyyy i am getting the same error but dont know how to fixxx . Please Help.  
this is my views.py

from django.shortcuts import render,redirect  
from .models import Post,BlogComment

# Create your views here.

from django.contrib import messages

def blogHome(request):  
blogposts = Post.objects.all()  
context = {  
‘post’: blogposts  
}  
return render(request, ‘blog/blogHome.html’)  
def blogPost(request,slug):  
blog = Post.objects.get(slug=slug)  
comments = BlogComment.objects.filter(post=blog)  
context = {  
‘post’: blog, ‘comments’: comments  
}  
# return HttpResponse(f"This is blogpost where particular blog is rendered {slug} ")  
return render(request, ‘blog/blogPost.html’)  
def postComment(request):  
if request.method ==“POST”:  
comment = request.POST.get(‘comment’)  
user = request.user  
postId = Post.POST.get(‘postId’)  
post = Post.objects.get(sno = postId)  
comment = BlogComment(comment=comment, user=user,post=post)  
comment.save()  
messages.warning(request, “your comment has been posted successfully”)  
# return HttpResponse(f"This is blogpost where particular blog is rendered {slug} ")  
return render(request,‘blog/blogPost.html’)

this is my models.py

from django.db import models  
from django.utils.timezone import now  
from django.contrib.auth.models import User

class Post(models.Model):  
postId = models.AutoField(primary\_key=True)

```
title = models.CharField(max_length=200, unique=True)
slug = models.CharField(max_length=200, unique=True, default="")
category = models.CharField(max_length=200, default="")

author = models.CharField(max_length=10, default="")
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
thumbnail = models.ImageField(upload_to="images/")

def __str__ (self):
    return self.title

```

class BlogComment(models.Model):  
sno = models.AutoField(primary\_key=True)  
comment = models.TextField()  
user = models.ForeignKey(User, on\_delete=models.CASCADE)  
post = models.ForeignKey(Post, on\_delete=models.CASCADE)  
parent = models.ForeignKey(‘self’, on\_delete=models.CASCADE, null=True)  
timestamp = models.DateTimeField(default=now)

this is the error when i added a new field in my model Post (postId)

django.db.utils.OperationalError: no such column: blog\_post.postId

Please help me fix it!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 20, 2023, 12:38pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/14 "2023-08-20T12:38:39Z")

</div>

This topic has been marked as solved. Your post is going to get more attention if you create a new topic for it.  
When posting code, copy/paste the code into the body of your message, between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

---

<div class="post-metadata">

**Author:** ![ThisisMS11](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thisisms11/32/16720_2.png) [@ThisisMS11](https://forum.djangoproject.com/u/ThisisMS11)\
**Post date:** [October 30, 2023, 6:12pm UTC](https://forum.djangoproject.com/t/problems-adding-database-columns/14716/15 "2023-10-30T18:12:56Z")

</div>

If someone is having a similar issue then they can follow the given steps which worked for me :

1. Identify the respective views and url file from the terminal error . For example in my case the error was something which included

```auto
File "/home/mohit/Desktop/Visual Studio Code/LEARN DJANGO/CE/trydjango/urls.py", line 20, in <module>
    from .views import home_view
  File "/home/mohit/Desktop/Visual Studio Code/LEARN DJANGO/CE/trydjango/views.py", line 10, in <module>
    article_obj = Article.objects.get(id=2)

```

for me it was views.py in trydjango and urls.py where i was trying to execute model during initialization.

1. To resolve this issue i simply commented out the all the code that required me to use the model that i have made recent changes to .

```auto
# """
# To render HTML Pages    
# """
# from django.http import HttpResponse
# from articles.models import Article
# from django.template.loader import render_to_string #,get_template

# # getting data from the sqlite in this form 
# # article_obj = Article.objects.get(id=2)
# # article_list = Article.objects.all()

# # the context to be passed on to the template has to be a dictionary

# myList = [100,200,300,400,500]
# context ={
# "article_list":article_list,
# "title": article_obj.title,
# "content": article_obj.content,
# "myList":myList
# }

# # Django Templates

# # using get_template 
# # tmpl = get_template('home-view.html')
# # tmpl_string = tmpl.render(context= context)
# # tmpl_string2 = tmpl.render(context= context)

# # using render_to_string
# HTML_STRING = render_to_string('home-view.html', context=context)

# # this way you can access the arguements , parameters passed in the url
# def home_view(request, id=None,*args,**kwargs):
# print(args,kwargs)
# print('id : ', id);  
# return HttpResponse(HTML_STRING)

```

also in urls.py

```auto
 # path("", home_view), commented the respective home view.

```

That worked for me . I was stuck at this issue for more than 2 hrs . I wish you don’t have to spend this long on it.

later i realised that even if we comment only the url path that uses this modified Model it seem to work the same way so you can do that only , no need to comment out the views.py .
