[英]django migration error message
I have the following models: 我有以下型号:
from django.db import models
from django.utils import timezone
# Create your models here.
class customer(models.Model):
# Need autoincrement, unique and primary
cstid = models.AutoField(primary_key=True, unique=True, default=1)
name = models.CharField(max_length=35)
age=models.IntegerField()
mobile=models.IntegerField()
class doctor(models.Model):
docid = models.AutoField(primary_key=True, unique=True) # Need autoincrement, unique and primary
name = models.CharField(max_length=35)
regid = models.CharField(max_length=15, default="", blank=True)
photo = models.CharField(
max_length=35, default="", blank=True)
email = models.EmailField(default="")
phone = models.IntegerField(default=0, blank=True)
qualifications = models.CharField(
max_length=50, default="", blank=True)
about = models.CharField(
max_length=35, default="", blank=True)
specialities = models.CharField(
max_length=50, default="", blank=True)
department = models.CharField(max_length=50, default="ENT", blank=True)
fees = models.FloatField(default=300.0)
displayfee = models.IntegerField(default=0, blank=True)
slotrange = models.CharField(max_length=50)
slotdurn = models.IntegerField(default=10)
breakrange = models.CharField(
max_length=50, default="", blank=True)
slotsleft = models.CharField(
max_length=50, default="", blank=True)
def __str__(self):
return self.name
class appointment(models.Model):
date = models.DateField(default=timezone.now)
time = models.TimeField(default=timezone.now)
docid = models.ForeignKey(doctor, on_delete=models.CASCADE, blank=True)
cstid = models.ForeignKey(customer, on_delete=models.CASCADE, blank=True)
CstSlot = models.CharField(max_length=10)
SlotsAvailable = models.CharField(max_length=40)
Durn = models.IntegerField()
During migration, I get the following errors: 迁移期间,出现以下错误:
You are trying to add a non-nullable field 'cstid' to appointment 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 with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>>
How can I fix these? 我该如何解决?
When you are defining a model field. 定义模型字段时。 By default
null=False
. 默认情况下,
null=False
。 So if null is prohibited you have to set default value. 因此,如果禁止使用null,则必须设置默认值。 Or you have to set
null=True
. 或者您必须设置
null=True
。
Option 1 选项1
Choose the option 1
while migrating, and provide some integer value that represents PK
of existing customer 迁移时选择
option 1
,并提供一些代表现有客户PK
的整数值
Option 2 选项2
Change your models.py
as any of the following method 通过以下任何一种方法更改您的
models.py
a) . 一种) 。 add
null=True
to model 添加
null=True
对模型正确
class appointment(models.Model):
date = models.DateField(default=timezone.now)
time = models.TimeField(default=timezone.now)
docid = models.ForeignKey(doctor, on_delete=models.CASCADE, blank=True)
cstid = models.ForeignKey(customer, on_delete=models.CASCADE, blank=True,
null=True)
CstSlot = models.CharField(max_length=10)
SlotsAvailable = models.CharField(max_length=40)
Durn = models.IntegerField()
b) . b)。 add
default=some_customer_id
to model 添加
default=some_customer_id
进行建模
class appointment(models.Model):
date = models.DateField(default=timezone.now)
time = models.TimeField(default=timezone.now)
docid = models.ForeignKey(doctor, on_delete=models.CASCADE, blank=True)
cstid = models.ForeignKey(customer, on_delete=models.CASCADE, blank=True,
default=1)
CstSlot = models.CharField(max_length=10)
SlotsAvailable = models.CharField(max_length=40)
Durn = models.IntegerField()
将您的cstid
替换为:
cstid = models.ForeignKey(customer, on_delete=models.CASCADE, blank=True, null=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.