﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27169	adding a field to a model with default value in postgres, when is migrated drop the default	rodo	nobody	"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
)

}}}



"	Bug	closed	Migrations	1.10	Normal	invalid	tutorial, model		Unreviewed	0	0	0	0	0	0
