Polls Not Null Constraint Failed

Hi,

I’ve created the polls app with the tutorial and I’m trying to test it. I’ve also created an API with it, and now I can’t save any new questions or choices. The error I get is “NOT NULL constraint failed: polls_question.owner_id”.

Here is my models.py:

from django.db import models
from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    owner = models.ForeignKey('auth.User', related_name='questions', on_delete=models.CASCADE)

    def __str__(self):
        return self.question_text
    
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

    def create(self, validated_data):
        return Question.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.question_text = validated_data.get('question_text', instance.question_text)
        instance.save()
        return instance

    def save(self, *args, **kwargs):
        super(Question, self).save(*args, **kwargs)

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    owner = models.ForeignKey('auth.User', related_name='choices', blank=True, default='', on_delete=models.CASCADE)
    
    def __str__(self):
        return self.choice_text

Here is my test.py:

import datetime
from django.utils import timezone
from django.urls import reverse

from .models import Question

class QuestionModelTests(TestCase):
    def test_was_published_recently_with_future_question(self):
        time = timezone.now() + datetime.timedelta(days=30)
        future_question =  Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

    def test_was_published_recently_with_old_question(self):
        time = timezone.now() - datetime.timedelta(days=1, seconds=1)
        old_question = Question(pub_date=time)
        self.assertIs(old_question.was_published_recently(), False)
    
    def test_was_published_recently_with_recent_question(self):
        time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
        recent_question = Question(pub_date=time)
        self.assertIs(recent_question.was_pulished_recently(), True)

def create_question(question_text, days):
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=time)

class QuestionIndexViewTests(TestCase):
    def test_no_questions(self):
        response = self.client.get(reverse('polls:index'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

    def test_past_question(self):
        create_question(question_text="Past question.", days=-30)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question.>']
        )

    def test_future_question(self):
        create_question(question_text="Future question.", days=30)
        response = self.client.get(reverse('polls:index'))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

    def test_future_question_and_past_question(self):
        create_question(question_text="Past question.", days=-30)
        create_question(question_text="Future question.", days=30)
        response = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question.>']
        )

    def test_two_past_questions(self):
        create_question(question_text="Past question 1.", days=-30)
        create_question(question_text="Past question 2.", days=-5)
        resposne = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question 2.>', '<Question: Past question 1.>']
        )

class QuestionDetailViewTests(TestCase):
    def test_future_question(self):
        future_question = create_question(question_text='Future question.', days=5)
        url = reverse('polls:detail', args=(future_question.id,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_past_question(self):
        past_question = create_question(question_text='Past Question.', days=-5)
        url = reverse('polls:detail', args=(past_question.id,))
        response = self.client.get(url)
        self.assertContains(response, past_question.question_text)

And here is my traceback:

----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: polls_question.owner_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\mysite\polls\tests.py", line 69, in test_future_question
    future_question = create_question(question_text='Future question.', days=5)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\mysite\polls\tests.py", line 26, in create_question
    return Question.objects.create(question_text=question_text, pub_date=time)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\query.py", line 447, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\mysite\polls\models.py", line 30, in save
    super(Question, self).save(*args, **kwargs)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\base.py", line 754, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\base.py", line 792, in save_base
    force_update, using, update_fields,
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\base.py", line 895, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\base.py", line 935, in _do_insert
    using=using, raw=raw,
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\query.py", line 1249, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\models\sql\compiler.py", line 1397, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Lisa\Documents\GitHub\pollsWithAPI\.venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: polls_question.owner_id

I’ve tried to re-run migrations, and I’ve played with null=True, blank=True in my models.py as well. I haven’t gotten anywhere.

Any advice is appreciated.

Thanks!

I’ve quoted the key parts of the information you’ve provided. You’re trying to create a Question object without supplying a parameter for the owner field. You can either include an owner in the create, or you can set null=True in the owner field, do a makemigrations and then a migrate.

Ken

1 Like

Thanks.

It worked with the first method, i.e. null=True

With the second method, what would I put?
return Question.objects.create(question_text=question_text, pub_date=time, owner=owner)
Would I need to specify owner before as well then, just like pub_date?

Yes, you could do something like:
owner = User.objects.get(username='Lisa')
to get a proper reference to a User object to pass to the Question object.
(That line would be inserted before the return statement.)

Ken

1 Like