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

时间:2016-12-04 21:51:05

标签: python django model-view-controller model

我有以下Django 1.10模型在运行时发出错误:

python manage.py makemigrations

  

...返回Database.Cursor.execute(self,query,params)   django.db.utils.IntegrityError:NOT NULL约束失败:steps_entry.project_id

我在这里做错了什么?

models.py代码:

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

编辑(完整追溯):

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

0 个答案:

没有答案