下面的代码编译,但每当我取消注释purchase_date,po_number,或确认它给我一个错误。在取消注释这些行之后我尝试了python manage.py syncdb,并且stil给了我错误。
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)
我得到的错误是:
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
这是否与已经创建的表格有关,它是否要我清除它们?
答案 0 :(得分:4)
虽然syncdb可用于创建新表,但它不适用于更改数据库表。在这种情况下,看起来您在运行syncdb一次后添加了3列。
这是the documentation(阅读:Syncdb不会改变现有表格)
要实现这一目标,您可以通过两种方式实现:
您需要第三方应用程序,例如django south,它将为您处理迁移。运行迁移后,您将能够毫无问题地访问这些列。 (强烈推荐)
如果您的代码尚未投入生产,您可以删除数据库然后执行syncdb
(重新开始) - 这不是非常推荐的 - 因为它可能是个好主意南。
这是南方的step by step tutorial,这里是official documentation on south