0

The code below this compiles, but whenever I uncomment purchase_date, po_number, or confirmed it's giving me an error. I tried python manage.py syncdb after uncommenting those lines and it's stil giving me errors.

from django.db import models


class PurchaseOrder(models.Model):

       product = models.CharField(max_length=256)
       vendor = models.CharField(max_length=256)
       price = models.FloatField()
       item_number = models.AutoField(primary_key=True)
    #  purchase_date = models.DateField()
    #  po_number = models.IntegerField(unique=True)
    #  confirmed = models.NullBooleanField(null=True)

The error I am getting is this:

DatabaseError at /admin/purchaseorders/purchaseorder/
column purchaseorders_purchaseorder.purchase_date does not exist
LINE 1: ...e", "purchaseorders_purchaseorder"."item_number", "purchaseo...
                                                             ^
Request Method: GET
Request URL:    
Django Version: 1.5.1
Exception Type: DatabaseError
Exception Value:    
column purchaseorders_purchaseorder.purchase_date does not exist
LINE 1: ...e", "purchaseorders_purchaseorder"."item_number", "purchaseo...
                                                             ^
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py in execute, line 54
Python Executable:  /usr/bin/python
Python Version: 2.7.3
Python Path:    
['/LPG/firstproject',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages']
Server time:    Tue, 23 Jul 2013 16:30:58 +0000

Does this have anything to do with tables already being created and it wants me to clear them?

4

1 回答 1

4

虽然 syncdb 可用于创建新表,但它不适用于更改数据库表。在这种情况下,看起来您在运行一次 syncdb 后添加了 3 列。

这是文档(阅读:Syncdb不会更改现有表)

为此,您可以通过 2 种方式来实现:

  1. 您将需要一个第三方应用程序,例如django south来为您处理迁移。运行迁移后,您将能够毫无问题地访问这些列。(强烈推荐)

  2. 如果您的代码尚未投入生产,您可以删除数据库然后执行syncdb(重新开始) - 这不是很推荐 - 因为使用 south 可能是个好主意。

这里是关于南方的一步一步教程,这里是关于南方的官方文档

于 2013-07-23T16:40:20.240 回答