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

644
Visualizações
Django implementation of default value in database

I had a field on a model with was:

class SomeModel(models.Model):
    some_field = models.CharField(max_length=10, null=True, blank=True)

Then I changed my model to:

class SomeModel(models.Model):
    some_field = models.CharField(max_length=10, default='')

When I ran django-admin sqlmigrate somemodels somemigration to check my migration I found the following changes:

ALTER TABLE "somemodels" ALTER COLUMN "some_field" SET DEFAULT '';
UPDATE "somemodels" SET "some_field" = '' WHERE "some_field" IS NULL;
ALTER TABLE "somemodels" ALTER COLUMN "some_field" SET NOT NULL;
ALTER TABLE "somemodels" ALTER COLUMN "some_field" DROP DEFAULT;

I am not understanding why the Django apply a DROP DEFAULT in the table since I am creating a default value. If this is correct, how does Django implement the default values?

Information about my tools:

  • Postgresql 9.5;
  • Django 1.11b1;
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The comments to django/db/backends/base/schema.py, starting ln. 571, detail the steps involved here:

When changing a column NULL constraint to NOT NULL with a given default value, we need to perform 4 steps:

  1. Add a default for new incoming writes
  2. Update existing NULL rows with new default
  3. Replace NULL constraint with NOT NULL
  4. Drop the default again.

Django does not usually use the built-in SQL default to set values (remember that Django can use callable values for defaults). You can find more information in this rejected bug report.

about 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