Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django makemigrations works, migrate fails with "django.db.utils.IntegrityError: NOT NULL constraint failed"

I'm stuck. Django 1.7, SQLite3.

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

and is now this:

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

I backed up all data to a json text file with

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

and then executed

python manage.py makemigrations

which worked. But

python manage.py migrate

fails with

django.db.utils.IntegrityError: NOT NULL constraint failed: article_article__new.thumbnail

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

What do I do?

like image 810
aliteralmind Avatar asked Aug 03 '14 21:08

aliteralmind


1 Answers

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

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

I then ran

python manage.py makemigrations

and

python manage.py migrate

successfully.

Thank goodness for irc.freenode.net/django.

like image 188
aliteralmind Avatar answered Oct 18 '22 15:10

aliteralmind