#27169: adding a field to a model with default value in postgres, when is 
migrated
drop the default
-------------------------------+-----------------------------
     Reporter:  rockdolfolk    |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Uncategorized  |    Version:  1.10
     Severity:  Normal         |   Keywords:  tutorial, model
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+-----------------------------
 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
 )

 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27169>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/054.1c163a5bf2f4d124ae06c38258def3fc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to