【问题标题】:Fixing null value in column "date_of_birth" violates not-null constraint修复“date_of_birth”列中的空值违反了非空约束
【发布时间】:2018-03-18 16:23:18
【问题描述】:

我正在构建一个电子健康记录软件。我的一个观点包含一个捕获患者基本信息的表格。

forms.py

from django import forms
from nesting.models import Identity_unique

class Identity_Form(forms.ModelForm):
    NIS = forms.CharField(
                    widget=forms.TextInput(
                            attrs={

                                'placeholder': 'Enter NIS',
                                'class' : 'form-control'
                            }
                )
    )
    First_Name = forms.CharField(
                widget=forms.TextInput(
                        attrs={

                            'placeholder': 'Enter First Name',
                            'class' : 'form-control'
                        }
            )
    )
    Last_Name = forms.CharField(

       widget=forms.TextInput(
               attrs={

                   'placeholder': 'Enter Last Name',
                   'class' : 'form-control'
               }
        )
    )
    Residence = forms.CharField(

       widget=forms.TextInput(
               attrs={

                   'placeholder': 'Enter Address',
                   'class' : 'form-control'
               }
        )
    )

    DOB = forms.CharField(

           widget=forms.TextInput(
                   attrs={

                       'placeholder': 'Enter Date of Birth',
                       'class' : 'form-control'
                   }
            )
        )
    class Meta:

        model = Identity_unique

        fields = ('NIS', 'First_Name', 'Last_Name', 'Residence', 'DOB',)

modelForm 的数据架构

models.py

from django.db import models
from django.contrib.auth.models import User
from Identity import settings
import datetime

# Create your models here.

class Identity_unique(models.Model):

    NIS = models.CharField(max_length = 200, primary_key = True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    Timestamp = models.DateTimeField(auto_now = True)
    First_Name = models.CharField(max_length = 80, null = True,  )
    Last_Name = models.CharField(max_length = 80, null = True, )
    Residence = models.CharField(max_length = 80, blank = True )
    DOB = models.DateField(auto_now = False)

不幸的是,我每次尝试将保存的数据提交到服务器时都会收到此错误:

IntegrityError at /nesting/
null value in column "date_of_birth" violates not-null constraint
DETAIL:  Failing row contains (Q234934, 2, 2017-10-06 19:17:42.084063+00, Andre, James, nou.self@gmail.com, null, 1991-12-10).

我正在使用 postgresql 9.6 作为数据库。我删除了迁移文件夹并多次迁移模型,但nullIntegrityError 仍然存在。我也一直在我的数据库中寻找数据库表 Identity_unique 但我找不到它。我这样做是为了删除具有空约束的列,因为我将生日字段从 date_of_birth = models.DateField(max_length = 100, default = 0) 更改为 DOB = models.DateField(auto_now = False). 正如您在错误中看到的那样,有一列带有 null,这是不允许的。

最初,当我使用date_of_birth = models.DateField(max_length = 100, null = True) 作为字段时。系统提示我添加默认值,我在 bash 终端中添加了 timezone.now()。我不确定这是否解释了错误背后的原因,但我仍在添加它。

【问题讨论】:

    标签: python mysql django bash postgresql


    【解决方案1】:

    “date_of_birth”列中的null 值违反了非空约束

    您可能没有将任何值保存到DOB 列,您需要允许NULL 值。你应该改成

    DOB = models.DateField(auto_now = False, null = True)
    

    您还应该确保表中的DOB 列允许null

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2016-02-14
      • 2017-02-18
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多