#30269: AlterField in database_operations of SeparateDatabaseAndState migration
doesn't set NOT NULL constraint for PostgreSQL
--------------------------------------+------------------------
               Reporter:  Dmitrii     |          Owner:  nobody
                   Type:  Bug         |         Status:  new
              Component:  Migrations  |        Version:  2.1
               Severity:  Normal      |       Keywords:
           Triage Stage:  Unreviewed  |      Has patch:  0
    Needs documentation:  0           |    Needs tests:  0
Patch needs improvement:  0           |  Easy pickings:  0
                  UI/UX:  0           |
--------------------------------------+------------------------
 For changing a field, and not having some unpredictable behavior we employ
 multi-stage deployments.

 So for example we have a field:
 {{{
 name = models.CharField(max_length=255, null=True)
 }}}

 And we want to make it non-nullable. To make it so we perform two
 migrations, firstly the state:

 {{{
     operations = [
         # Plus whatever data migration is needed for the NULL values
         migrations.SeparateDatabaseAndState(
             state_operations=[
                 migrations.AlterField(
                     model_name='testmodel',
                     name='name',
                     field=models.CharField(max_length=255)
                 )
             ]
         )
     ]
 }}}

 After all parts of the system have been updated with code that handles the
 given field only in a non-nullable way, we can safely set it to NON NULL
 in the database:

 {{{
     operations = [
         migrations.SeparateDatabaseAndState(
             database_operations=[
                 migrations.AlterField(
                     model_name='testmodel',
                     name='name',
                     field=models.CharField(max_length=255)
                 )
             ]
         )
     ]
 }}}

 But the last migration doesn't actually perform any changes, the field
 remains nullable in the database. This can be circumvented by using
 migrations.RunSQL, but one can easily miss out the need to do that.

 PostgreSQL in use: 9.6 official docker image.
 Originally found for Django 1.8, reproduced for Django 2.1.7.

 Didn't reproduce with SQLite.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30269>
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/051.c3cd34ceeddef994d334dd337828f133%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to