【发布时间】:2016-01-04 16:03:49
【问题描述】:
我正在使用命令python manage.py makemigrations
但是,我收到此错误:
You are trying to add a non-nullable field 'id' to contact_info without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows) 2) Quit, and let me add a default in models.py
这里是models.py:
class Document(models.Model):
docfile = models.FileField(upload_to='documents/')
class contact_number(models.Model):
contact_numbers = models.CharField(max_length=13)
class contact_address(models.Model):
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zip = models.CharField(max_length=5)
class contact_info(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
contact_numbers = models.ManyToManyField(contact_number)
addresses = models.ManyToManyField(contact_address)
【问题讨论】:
-
这是特定的,但我不知道如何更正此问题或将值放入 db。
-
您之前是否设置了不同的字段作为主键?我很惊讶迁移正在尝试添加一个 id 字段。
-
如果数据库中没有任何重要数据,那么最简单的方法可能是删除数据库并重新创建它,删除应用程序的所有迁移,然后重新运行
makemigrations。 -
我清除了数据库,然后重新启动syncdb,但错误仍然相同:(
-
不要使用syncdb,它已被弃用。清除数据库,删除所有迁移,然后重新创建它们。
标签: django python-2.7 django-models