修改Django的models.py后出现1054错误:erp_app_invoice表无id列
问题描述
修改models.py后出现错误:(1054, "Unknown column 'id' in 'erp_app_invoice'")。明明Django会自动生成id主键字段,自己没在模型里定义,尝试手动添加id = models.AutoField(primary_key=True)提示不能有两个AutoField,改成CharField也没用。
模型代码(models.py)
class Invoice(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE, default=None, editable=False) created_at = models.DateTimeField(auto_now_add=True) first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) phone = models.CharField(max_length=50) address = models.CharField(max_length=50) city = models.CharField(max_length=50) product1 = models.CharField(max_length=50) product1_qty = models.CharField(max_length=3) product1_price = models.CharField(max_length=50) product1_total = models.CharField(max_length=50, blank=True) product2 = models.CharField(max_length=50, blank=True) product2_qty = models.CharField(max_length=3, blank=True) product2_price = models.CharField(max_length=50, blank=True) product2_total = models.CharField(max_length=50, blank=True) product3 = models.CharField(max_length=50, blank=True) product3_qty = models.CharField(max_length=3, blank=True) product3_price = models.CharField(max_length=50, blank=True) product3_total = models.CharField(max_length=50) shipping_fees = models.CharField(max_length=50) sub_total = models.CharField(max_length=50, blank=True) discount = models.CharField(max_length=50, blank=True) total_order = models.CharField(max_length=50) def __str__(self): return(f"{self.first_name} {self.last_name} | {self.phone}")
模板代码(HTML)
{% extends "base.html" %} {% block content%} <h1>My Invoices</h1> <br/><br/> <a type="button" href="{% url 'services' %}" class="btn btn-secondary">Back</a> <a type="button" href="{% url 'add_invoice' %}" class="btn btn-success">Add Invoice</a> <br/><br/> <table class="table table-striped table-hover table-bordered"> <thead class="table-dark"> <tr> <th scope="col">ID</th> <th scope="col">Date</th> <th scope="col">Name</th> <th scope="col">Phone</th> <th scope="col">Address</th> <th scope="col">Total</th> </tr> </thead> <body> {% if invoices %} {% for invoice in invoices %} <tr> <td><a href="{% url 'invoice' invoice.id %}">{{ invoice.id }}</a></td> <td>{{ invoice.created_at }}</td> <td>{{ invoice.first_name }} {{ invoice.last_name }}</td> <td>{{ invoice.phone }}</td> <td>{{ invoice.address }} ,{{ invoice.city }}</td> <td>{{ invoice.total_order }}</td> </tr> {% endfor %} {% endif %} </tbody> </table> {% endblock %}
视图代码
def invoice(request): if request.user.is_authenticated: invoices = Invoice.objects.filter(owner=request.user) return render(request, 'invoices.html', {'invoices':invoices}) else: messages.success(request, "You Must Be Logged In To View This Page !") return redirect('login')
解决方案
这个问题本质是数据库表与Django模型不同步导致的,Django默认生成id主键,但如果之前的表没有该字段,或修改模型后迁移未正确执行,就会报错。按以下步骤解决:
生成并执行迁移(推荐)
- 生成迁移文件:
python manage.py makemigrations - 执行迁移同步数据库:
python manage.py migrate
- 生成迁移文件:
迁移无效时手动补全字段
如果迁移命令未生效,直接在数据库中给erp_app_invoice表添加id字段:ALTER TABLE erp_app_invoice ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY;执行后重启Django服务再测试。
排查自定义主键
检查模型中是否有其他字段被设置为primary_key=True,如果有,Django不会自动生成id字段。当前模型无此情况,可跳过。
内容的提问来源于stack exchange,提问作者beshoy saleh
相关产品推荐
相关产品推荐

