0

I have the following Django 1.10 model that is giving an error when I run:

python manage.py makemigrations

... return Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError: NOT NULL constraint failed: steps_entry.project_id

What am I doing wrong here?

The models.py code:

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Project(models.Model):
    project_title = models.CharField(max_length=200)
    created_date = models.DateTimeField('date created')
    def __str__(self):
        return self.project_title

class Entry(models.Model):
    title = models.CharField(max_length=200)
    REFERENCE = 'reference'
    BACKBURNER = 'backburner-item'
    ACTION_STEP = 'action-step'
    CONTAINER_CHOICES = (
        (REFERENCE, 'Reference'),
        (BACKBURNER, 'Backburner'),
        (ACTION_STEP, 'Action'),
    )
    container = models.CharField(max_length=200, choices=CONTAINER_CHOICES, default=ACTION_STEP)

    project = models.ForeignKey('Project', on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.title

Edit (full traceback):

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/steps/entry/

Django Version: 1.10.3
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'steps.apps.StepsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  544.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  211.             return view(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1657.             selection_note=_('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},

File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __len__
  238.         self._fetch_all()

File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _fetch_all
  1087.             self._result_cache = list(self.iterator())

File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __iter__
  54.         results = compiler.execute_sql()

File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  835.             cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  337.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /admin/steps/entry/
Exception Value: no such column: steps_entry.project_id
7
  • Does the database already exist? Commented Dec 4, 2016 at 21:58
  • Yes. In fact, I can inspect the Project objects in the Admin alright. The problem is with the Entry objects. Commented Dec 4, 2016 at 22:01
  • Please show the full traceback Commented Dec 4, 2016 at 22:04
  • I did something (emptied the migrations directory), so now it returns this: Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: No migrations to apply. However, when I go to inspect Entry objects, I get the following in the browser (see edit). Commented Dec 4, 2016 at 22:07
  • 2
    It's not necessarily a glitch. The most probable explanation is that you tried to make a previously nullable column not null, and the table had rows with null values in that column. Commented Dec 5, 2016 at 13:06

1 Answer 1

2
NOT NULL constraint failed: steps_entry.project_id

Simply you've to add null=true & blank=true in your models .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.