繁体   English   中英

django迁移错误消息

[英]django migration error message

我有以下型号:

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()

迁移期间,出现以下错误:

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
>>> 

我该如何解决?

定义模型字段时。 默认情况下, null=False 因此,如果禁止使用null,则必须设置默认值。 或者您必须设置null=True

选项1
迁移时选择option 1 ,并提供一些代表现有客户PK的整数值

选项2
通过以下任何一种方法更改您的models.py
一种) 。 添加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)。 添加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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM