'Django OperationalError at new model object
I created a system with Django. My model was working until I added a new field but when I add new field it gives an error. My new field is owner_id
OperationalError at /pdfs/ no such column: financial_analysis_pdf.owner_id_id
How Can I fix it?
models.py
class Pdf(models.Model):
CHOICES = [
..
]
STAT = [
...
]
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=200)
pdf = models.FileField(upload_to=user_directory_path)
type = models.CharField(max_length=200, default='Select', choices=CHOICES)
year = models.CharField(max_length=200, default='Select')
created_date = models.DateTimeField(default=datetime.now())
owner_id = models.ForeignKey(Customer, on_delete=models.CASCADE)
def __str__(self):
return self.title
pdfs.html
<tbody id="myTable">
{% for pdf in pdfs %}
<tr>
<td><a href="/ocr" >{{ pdf.title }} </a></td>
<td>{{ pdf.year }}</td>
<td>{{ pdf.type }}</td>
<td> <i class="fas fa-check-circle" style="color: #71ca51"></i></td>
<td>
<a href="{{ pdf.pdf.url }}" class="btn btn-primary btn-sm" target="_blank">Download</a>
</td>
<td>
<form method="post" action="{% url 'pdf_delete' pdf.pk %}">
{% csrf_token %}
<button class="btn btn-danger btn-sm" type="submit" onclick="return confirm('Are you sure you want to delete this analyse?');"><i class="fas fa-trash"></i></button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
views.py
def pdf_list(request):
pdfs = Pdf.objects.all().order_by('-year')
return render(request, 'pdf_list.html', {'pdfs': pdfs})
customer/model.py
Class Customer(models.Model):
customer_name = models.CharField(max_length=20)
...
id = models.AutoField(primary_key=True)
traceback
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/pdfs/
Django Version: 3.1.4
Python Version: 3.8.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'register',
'customer',
'financial_analysis',
'ocr',
'crispy_forms',
'ckeditor']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template C:\Users\USER\OneDrive\Documents\GitHub\otc\templates\base.html, error at line 0
no such column: financial_analysis_pdf.owner_id_id
1 : <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
2 : <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
3 : <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
4 : <!------ Include the above in your HEAD tag ---------->
5 :
6 : <!DOCTYPE html>
7 : <html lang="en">
8 :
9 : <head>
10 : {% load static %}
Traceback (most recent call last):
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
The above exception (no such column: financial_analysis_pdf.owner_id_id) was the direct cause of the following exception:
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\financial_analysis\views.py", line 8, in pdf_list
return render(request, 'pdf_list.html', {'pdfs': pdfs})
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\template\defaulttags.py", line 168, in render
len_values = len(values)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\models\query.py", line 269, in __len__
self._fetch_all()
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\models\query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\USER\OneDrive\Documents\GitHub\otc\myvenv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /pdfs/
Exception Value: no such column: financial_analysis_pdf.owner_id_id
base.html
<li class="sidebar-dropdown">
{% load customer_list %}
{% get_customers request.user as customers %}
{% for customer in customers %}
<a href="#">
<i class="fa fa-building"></i>
<span>{{customer.customer_name}}</span>
</a>
<div class="sidebar-submenu">
<ul>
<li>
<a href="/customers/{{ customer.id }}/profile">View</a>
</li>
<li>
<a href="/customers/{{ customer.id }}/update"> Update</a>
</li>
<li>
<a href="/pdfs">Analyses</a>
</li>
</ul>
</div>
{% endfor %}
</li>
Solution 1:[1]
when we update our models in django we have to give a default value for already existing objects. so you can follow the below step.
- Update models with a default value like below -
owner_id = models.ForeignKey(Customer, on_delete=models.CASCADE, default="")
Now you after saving this you can run command (if you're using VS Code)- python manage.py makemigrations
The last step to run the command - python manage.py migrate.
After following these step you should not get the error as before.
Solution 2:[2]
I think the issue is that you're supposed to refer to financial_analysis.owner_id.id
. Note the full-stop as opposed to the underscore.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rakhi Yadav |
Solution 2 | Ahmad Muhammad |