Opened 8 years ago

Closed 8 years ago

#27169 closed Bug (invalid)

adding a field to a model with default value in postgres, when is migrated drop the default

Reported by: rodo Owned by: nobody
Component: Migrations Version: 1.10
Severity: Normal Keywords: tutorial, model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When I was studying the tutorial, I forgot the field "vote" in the model Choise, so I defined later and done a new migration:

(the development is in spanish)

class Eleccion(models.Model):
    pregunta = models.ForeignKey(Pregunta, on_delete=models.CASCADE)
    eleccion_text = models.CharField(max_length=200)
    votos = models.IntegerField(default=0)   <=== new field

These are the log of the migration:

python manage.py makemigrations encuestas
Migrations for 'encuestas':
  encuestas\migrations\0002_eleccion_votos.py:
    - Add field votos to eleccion


python manage.py sqlmigrate encuestas 0002
BEGIN;
--
-- Add field votos to eleccion
--
ALTER TABLE "encuestas_eleccion" ADD COLUMN "votos" integer DEFAULT 0 NOT NULL;
ALTER TABLE "encuestas_eleccion" ALTER COLUMN "votos" DROP DEFAULT;          <=== ???
COMMIT;


python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, encuestas, sessions
Running migrations:
  Rendering model states... DONE
  Applying encuestas.0002_eleccion_votos... OK

This is the final table:

CREATE TABLE encuestas.encuestas_eleccion
(
  id serial NOT NULL,
  eleccion_text character varying(200) NOT NULL,
  pregunta_id integer NOT NULL,
  votos integer NOT NULL,     <=== it has not default value
  CONSTRAINT encuestas_eleccion_pkey PRIMARY KEY (id),
  CONSTRAINT encuestas_eleccio_pregunta_id_9558286d_fk_encuestas_pregunta_id FOREIGN KEY (pregunta_id)
      REFERENCES encuestas.encuestas_pregunta (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)

Change History (1)

comment:1 by Tim Graham, 8 years ago

Component: UncategorizedMigrations
Resolution: invalid
Status: newclosed
Type: UncategorizedBug

That's correct, Django doesn't use database defaults except to set values on existing database rows.

Note: See TracTickets for help on using tickets.
Back to Top