Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

353
Views
What's the reason for IntegrityError: NOT NULL constraint failed?

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

  • So if null=False(default), django will use empty string (means not NULL)
  • If 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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!