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

19,047

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.

Share:
19,047
aliteralmind
Author by

aliteralmind

Backend Java, regular expressions, text processing, and object-oriented design are my strengths. I have five years experience with Oracle SQL, stored procedures, and triggers, and I am the author of Codelet (automated insertion of example code into JavaDoc). My resume. Notable questions and answers: Improving upon Bloch's Builder Pattern for highly-extendable classes From every-time checks, to Functors, to Java 8 Lambdas Validating number-ranges w/ regex (a bad idea, but instructive) Contributor to and maintainer of the Regex-Fu FAQ Mega Wiki Practical and performance differences between for vs. foreach in Java I am also a singer and a Muppets super-fan.

Updated on June 08, 2022

Comments

  • aliteralmind
    aliteralmind almost 2 years

    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?