You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django报错OperationalError:polls_bands.id列不存在,执行迁移仍未解决

Fixing django.db.utils.OperationalError: no such column: polls_bands.id

Hey Ethan, let's work through this error together—it's a super common migration gotcha in Django, so we'll walk through the most likely fixes step by step:

  • First, double-check your Bands model definition
    Django automatically adds an id primary key field to every model unless you explicitly define a custom primary key (like band_id = models.AutoField(primary_key=True)). If you recently changed the primary key field name or removed it accidentally, that would cause Django to look for a column that doesn't exist.
    Example of a valid model with the default id field:

    from django.db import models
    
    class Bands(models.Model):
        name = models.CharField(max_length=100)
        # Django auto-creates an `id` primary key here—no need to define it
    
  • Confirm migrations were actually applied
    Run this command to check the status of your polls app migrations:

    python manage.py showmigrations polls
    

    Look for an [X] next to all migration files—if any are unmarked, run python manage.py migrate polls to apply the missing migrations specifically to the polls app.

  • Reset stale or missing migration files
    Sometimes makemigrations fails to generate a migration for subtle changes, or old migrations can get out of sync. If you don't have critical data in your database (or you've backed it up), try:

    1. Delete all files in polls/migrations/ except __init__.py
    2. Re-generate and apply migrations:
      python manage.py makemigrations polls
      python manage.py migrate polls
      
  • Check for manual database edits
    If you modified the polls_bands table directly via SQL (instead of using Django migrations), the database and your model definitions will be out of sync. Avoid manual DB edits whenever possible, but if you did, back up any important data, drop the polls_bands table, then re-run migrations to rebuild it.

  • Verify app and model naming
    Make sure your app is listed as polls in INSTALLED_APPS in settings.py, and your model is correctly named Bands in polls/models.py. Typos here can lead Django to look for a non-existent table or column.

If none of these resolve the issue, share your full Bands model code and the output of python manage.py showmigrations polls—that'll help us dig deeper.

内容的提问来源于stack exchange,提问作者Ethan Pearce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:23:24