Opened 9 years ago

Last modified 9 years ago

#24817 closed Bug

Renaming a model field that has null=False makes it nullable in MySQL — at Version 1

Reported by: murfi Owned by: nobody
Component: Migrations Version: 1.8
Severity: Release blocker Keywords:
Cc: alasdair@…, me@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by murfi)

Steps to reproduce:

  1. Create a simple model:
    class TestModel(models.Model):
            value_a = models.IntegerField(null=False)
    
  2. ./manage.py makemigrations
  3. ./manage.py migrate
  4. So far so good:
    mysql> describe testy_testmodel;
    +---------+---------+------+-----+---------+----------------+
    | Field   | Type    | Null | Key | Default | Extra          |
    +---------+---------+------+-----+---------+----------------+
    | id      | int(11) | NO   | PRI | NULL    | auto_increment |
    | value_a | int(11) | NO   |     | NULL    |                |
    +---------+---------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)
    
  5. Change model field name value_a -> value_b:
    class TestModel(models.model):
            value_b = models.IntegerField(null=False)
    
  6. ./manage.py makemigrations
  7. ./manage.py migrate
  8. NOT NULL for value_b has been lost:
    mysql> describe testy_testmodel;
    +---------+---------+------+-----+---------+----------------+
    | Field   | Type    | Null | Key | Default | Extra          |
    +---------+---------+------+-----+---------+----------------+
    | id      | int(11) | NO   | PRI | NULL    | auto_increment |
    | value_b | int(11) | YES  |     | NULL    |                |
    +---------+---------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)
    

The ALTER-statement django produces is

ALTER TABLE `testy_testmodel` CHANGE `value_a` `value_b` integer;

...and it should contain the NOT NULL, but it doesn't.

This affects other ALTER TABLE-statements too, e.g. if changing field type from IntegerField to BigIntergerField. The bug is present in 1.7 as well.

Change History (1)

comment:1 by murfi, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top