IntegrityError:NOT NULL约束失败,默认= None,空白= True null =模型中为True?

时间:2016-06-17 13:58:22

标签: python django sqlite

我在django shell中运行我的脚本,错误来自以下几行:

new_species = Species(genus=new_genus, species=row[4])
new_species.save()

此错误:

  

IntegrityError:NOT NULL约束失败:   birds_species.species_english

我正在尝试将信息保存到此模型中:

class Species(models.Model):
    genus = models.ForeignKey(Genus, default=None)
    species = models.CharField(max_length=100, default=None)
    species_english = models.CharField(max_length=100, default=None, blank=True, null=True)

    def __str__(self):
        return self.species

我确认迁移是最新的,因为我在django项目中运行以下行:

$ python manage.py makemigrations birds
No changes detected in app 'birds'
$ python manage.py migrate
Operations to perform:
  Apply all migrations: sessions, admin, polls, auth, contenttypes, birds
Running migrations:
  No migrations to apply.

问题是我试图将对象保存到Species模型,而没有定义1列species_english。从模型可以看出,我有default=None, blank=True, null=True这是我认为默认情况下我们需要允许空值。我有什么不对吗?

我正在使用默认的sqlite3 DB作为数据库后端。

谢谢!

修改

以下是迁移文件内容:

# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-06-17 14:02
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('birds', '0007_speciesfile'),
    ]

    operations = [
        migrations.AddField(
            model_name='species',
            name='species_english',
            field=models.CharField(blank=True, default=None, max_length=100, null=True),
        ),
    ]

0 个答案:

没有答案