Django is asking to delete column, which does not even exists

I tried applying migrations, but django ask me to delete a column which does not exist. Please help

The error which i get is - OperationalError at /admin/location/location/ (1054, "Unknown column 'location_location.id' in 'field list'")

models.py

class Location(models.Model):
    city = models.CharField(max_length=255)
    state = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)

views.py

def add_location(request):
    if request.method == 'POST':
        form = LocationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')
    else:
        form = LocationForm()
    return render(request, 'add_location.html', {'form': form})

admin.py

from django.contrib import admin
from .models import Location

# Register your models here.
admin.site.register(Location)

I tried using migrations --fake but it made it even worse. Please help me out. this is urgent :(

Traceback Traceback (most recent call last):

File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute return self.cursor.execute(sql, params) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute return self.cursor.execute(query, args) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 206, in execute res = self._query(query) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 319, in _query db.query(q) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py", line 254, in query _mysql.connection.query(self, query)

The above exception ((1054, "Unknown column 'location_location.id' in 'field list'")) was the direct cause of the following exception:
File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\admin\options.py", line 686, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 133, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\decorators\cache.py", line 62, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\admin\sites.py", line 242, in inner return view(request, *args, **kwargs) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 46, in _wrapper return bound_method(*args, **kwargs) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 133, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\admin\options.py", line 2068, in changelist_view "selection_note": _("0 of %(cnt)s selected") % {"cnt": len(cl.result_list)}, File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 376, in len self._fetch_all() File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 1867, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 87, in iter results = compiler.execute_sql( File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\sql\compiler.py", line 1398, in execute_sql cursor.execute(sql, params) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 103, in execute return super().execute(sql, params) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 67, in execute return self._execute_with_wrappers( File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers return executor(sql, params, many, context) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute with self.db.wrap_database_errors: File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py", line 91, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute return self.cursor.execute(sql, params) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute return self.cursor.execute(query, args) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 206, in execute res = self._query(query) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py", line 319, in _query db.query(q) File "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py", line 254, in query _mysql.connection.query(self, query)

Exception Type: OperationalError at /admin/location/location/ Exception Value: (1054, "Unknown column 'location_location.id' in 'field list'")

Simply try this:

first, delete migration file related to that Location models and re-migrate manually using below commands:

python manage.py makemigrations appname

python manage.py sqlmigrate appname 0001 # this value will generate after makemigration. It can be either 0001 or 0002 and so on.

python manage.py migrate

It's possible that you are trying to delete a column that was previously added to your model, but the migration to add the column to the database was not completed or was rolled back. In this case, Django is trying to delete a column that does not exist in the database.

To fix this issue, you can try the following steps:

  1. Make sure that you have the latest version of your code and that all migrations have been applied to the database.

  2. If the column does not exist in the database, you can try deleting the migration file that adds the column and then running the makemigrations and migrate commands again.

  3. If the column does exist in the database, but you no longer need it, you can remove the field from your model and then run the makemigrations and migrate commands to delete the column from the database.

It's important to note that deleting a column from the database can cause data loss, so you should be careful when doing this and make sure that you have a backup of your data before proceeding.

Back to Top