Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

354
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda