Django迁移要求删除不存在列,出现location_location.id不存在错误
问题描述
执行Django迁移时系统要求删除不存在的列,访问/admin/location/location/路径触发OperationalError,错误信息:(1054, "Unknown column 'location_location.id' in 'field list'")。尝试使用--fake选项后问题加重,完整报错栈如下。
相关代码
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)
完整报错栈(已翻译)
追溯(最近的调用最后): 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py",第89行,在 _execute 中 return self.cursor.execute(sql, params) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py",第75行,在 execute 中 return self.cursor.execute(query, args) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py",第206行,在 execute 中 res = self._query(query) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py",第319行,在 _query 中 db.query(q) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py",第254行,在 query 中 _mysql.connection.query(self, query) 上述异常 ((1054, "Unknown column 'location_location.id' in 'field list'")) 是以下异常的直接原因: 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py",第55行,在 inner 中 response = get_response(request) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py",第197行,在 _get_response 中 response = wrapped_callback(request, *callback_args, **callback_kwargs) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\admin\options.py",第686行,在 wrapper 中 return self.admin_site.admin_view(view)(*args, **kwargs) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py",第133行,在 _wrapped_view 中 response = view_func(request, *args, **kwargs) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\views\decorators\cache.py",第62行,在 _wrapped_view_func 中 response = view_func(request, *args, **kwargs) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\admin\sites.py",第242行,在 inner 中 return view(request, *args, **kwargs) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py",第46行,在 _wrapper 中 return bound_method(*args, **kwargs) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py",第133行,在 _wrapped_view 中 response = view_func(request, *args, **kwargs) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\admin\options.py",第2068行,在 changelist_view 中 "selection_note": _("0 of %(cnt)s selected") % {"cnt": len(cl.result_list)}, 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py",第376行,在 __len__ 中 self._fetch_all() 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py",第1867行,在 _fetch_all 中 self._result_cache = list(self._iterable_class(self)) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py",第87行,在 __iter__ 中 results = compiler.execute_sql( 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\sql\compiler.py",第1398行,在 execute_sql 中 cursor.execute(sql, params) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py",第103行,在 execute 中 return super().execute(sql, params) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py",第67行,在 execute 中 return self._execute_with_wrappers( 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py",第80行,在 _execute_with_wrappers 中 return executor(sql, params, many, context) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py",第84行,在 _execute 中 with self.db.wrap_database_errors: 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py",第91行,在 __exit__ 中 raise dj_exc_value.with_traceback(traceback) from exc_value 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py",第89行,在 _execute 中 return self.cursor.execute(sql, params) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\mysql\base.py",第75行,在 execute 中 return self.cursor.execute(query, args) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py",第206行,在 execute 中 res = self._query(query) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\cursors.py",第319行,在 _query 中 db.query(q) 文件 "C:\Users\sarthak.lamba\AppData\Local\Programs\Python\Python311\Lib\site-packages\MySQLdb\connections.py",第254行,在 query 中 _mysql.connection.query(self, query) 异常类型:OperationalError 在 /admin/location/location/ 异常值:(1054, "字段列表中不存在列 'location_location.id'")
解决办法
核心原因
Django默认会给每个模型自动添加id主键列,但你的数据库表location_location里没有这个列,导致ORM查询时出错。加上之前用--fake选项错误同步了迁移状态,进一步加剧了模型与数据库的不一致。
步骤1:修正迁移状态与数据库的不一致
- 查看当前迁移记录:
python manage.py showmigrations location
- 回退到初始迁移状态(生产环境需先备份数据):
python manage.py migrate location zero --fake
步骤2:手动添加缺失的id列
登录MySQL数据库,执行SQL语句:
ALTER TABLE location_location ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY;
步骤3:重新生成并应用迁移
- 删除
location应用下migrations目录中除__init__.py外的所有文件 - 生成初始迁移:
python manage.py makemigrations location
- 应用迁移(不要用
--fake):
python manage.py migrate location
备选方案(开发环境专用)
如果是开发环境且数据不重要,可直接重建表:
- 登录MySQL删除表:
DROP TABLE location_location;
- 重新生成并应用迁移:
python manage.py makemigrations location python manage.py migrate location
内容的提问来源于stack exchange,提问作者n0cuous
相关产品推荐
相关产品推荐

