我尝试应用迁移,但是django要求我删除一个不存在的列。
我得到的错误是
位于/admin/location/的操作错误(1054,“在'字段列表'中存在未知列'location_location.id'”)
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)
我试过使用迁移--假的,但它使情况更糟。
追溯追溯(最近调用最后调用):
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'")
2条答案
按热度按时间cgh8pdjw1#
简单地试试这个:
首先,删除与
Location
型号相关迁移文件,然后使用以下命令手动重新迁移:5kgi1eie2#
有可能您正在尝试删除一个先前添加到模型中的列,但是将该列添加到数据库中的迁移没有完成或被回滚。在这种情况下,Django正在尝试删除一个在数据库中不存在的列。
要解决此问题,您可以尝试以下步骤:
1.确保您拥有最新版本的代码,并且所有迁移都已应用到数据库。
1.如果数据库中不存在该列,则可以尝试删除添加该列的迁移文件,然后再次运行
makemigrations
和migrate
命令。1.如果数据库中确实存在该列,但您不再需要它,则可以从模型中删除该字段,然后运行
makemigrations
和migrate
命令从数据库中删除该列。请务必注意,从数据库中删除列可能会导致数据丢失,因此在执行此操作时应小心,并确保在继续之前备份了数据。