Django makemigrations有效,迁移失败并显示"django.db.utils.IntegrityError:NOT NULL约束失败". [英] Django makemigrations works, migrate fails with "django.db.utils.IntegrityError: NOT NULL constraint failed"

查看:37
本文介绍了Django makemigrations有效,迁移失败并显示"django.db.utils.IntegrityError:NOT NULL约束失败".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了.Django 1.7,SQLite3.

I'm stuck. Django 1.7, SQLite3.

我已更改模型以添加 thumbnail 列,如

I've changed my model to add the thumbnail column, as in this tutorial. It was this:

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    pub_date = models.DateTimeField('date published')
    likes = models.IntegerField(default=0)

    def __str__(self):
        return  self.title

现在是这个:

from django.db import models
from time import time         

def get_upload_file_name(instance, filename):
    return  "uploaded_files/%s_%s" % (str(time()).replace(".", "_"), filename)

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    pub_date = models.DateTimeField('date published')
    likes = models.IntegerField(default=0) 
    thumbnail = models.FileField(upload_to=get_upload_file_name, null=True)

    def __str__(self):
        return  self.title

我使用

python manage.py dumpdata article --indent=4 > article.json

然后执行

python manage.py makemigrations

有效.但是

python manage.py migrate

失败

django.db.utils.IntegrityError:NOT NULL约束失败:article_article__new.thumbnail

现在,即使将 null = True 添加到 models.py 中的 thumbnail 行中,也运行了 makemigrations 成功,并且 migrate 失败的方式相同.

And now, even after adding null=True to the thumbnail line in models.py, running makemigrations succeeds, and migrate fails the same way.

我该怎么办?

推荐答案

我的应用程序名称(由 python manage.py startapp 创建)称为 articles .在多次出现空约束错误之后,这是新的 articles \ migrations 文件夹:

My app name (as created with python manage.py startapp) is called articles. Here is the new articles\migrations folder, after getting the null-constraint error multiple times:

__init__.py
0001_initial.py
0002_auto_20140803_1540.py
0003_auto_20140803_1542.py
0004_auto_20140803_1450.py
0005_auto_20140803_1552.py
__pycache__
   __init__.cpython-34.pyc
   0001_initial.cpython-34.pyc
   0002_auto_20140803_1540.cpython-34.pyc
   0003_auto_20140803_1542.cpython-34.pyc
   0004_auto_20140803_1450.cpython-34.pyc
   0005_auto_20140803_1552.cpython-34.pyc

我删除了两个目录中的所有000 *文件,除了0001.

I deleted all the 000* files, in both directories, except 0001.

然后我跑了

python manage.py makemigrations

python manage.py migrate

成功.

非常感谢 irc.freenode.net/django .

这篇关于Django makemigrations有效,迁移失败并显示"django.db.utils.IntegrityError:NOT NULL约束失败".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆