I'm doing the polls apps in Django Documentation: Writing your first Django app, part 2. While working with Database API, I've found an IntegrityError.
polls/models.py
class Question(models.Model):
question_text=models.CharField(max_length=200)
pub_date=models.DateTimeField('date published')
I've tried to create an object of Question model.
Question.objects.create(question_text="What's up?")
This gives an error
IntegrityError: NOT NULL constraint failed: polls_question.pub_date
But when i try this
Question.objects.create(pub_date=timezone.now())
It creates an object successfully.
What's the reason for IntegrityError in the first case and why it doesn't generate any error in the second case?
From django doc
Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.
null=False(default), django will use empty string (means not
NULL)null=True, Field can write NULL (means None comes to python) So in brief:
Django will not raise IntegrityError: NOT NULL constraint for CharField and TextField.