Django alterField doesn't keep NOT NULL when max_length changes
I am writing a django migration to change max_length to CharField in the following model from 200 to 255, but doing so changes those fields from non-nullable to nullable. I want these fields to remain non-null.
With sqlmigrate
I can see the sql that django runs for every migration:
Model definition
# -*- coding: utf-8 -*-
from django.db import models
class Account(models.Model):
# Would be added by Django default anyways.
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
email = models.EmailField(max_length=200)
password = models.CharField(max_length=200)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
last_seen_at = models.DateTimeField(auto_now_add=True)
project_token = models.CharField(max_length=200)
url = models.URLField(max_length=200)
notification_emails = models.TextField(blank=True)
Initial migration 0001
BEGIN;
CREATE TABLE `pnmodels_account` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(200) NOT NULL, `email` varchar(200) NOT NULL, `password` varchar(200) NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `last_seen_at` datetime NOT NULL, `project_token` varchar(200) NOT NULL, `url` varchar(200) NOT NULL, `notification_emails` longtext NOT NULL);
COMMIT;
Migration 0002 to update max_length to 255
BEGIN;
ALTER TABLE `pnmodels_account` MODIFY `email` varchar(255);
ALTER TABLE `pnmodels_account` MODIFY `name` varchar(255);
ALTER TABLE `pnmodels_account` MODIFY `password` varchar(255);
ALTER TABLE `pnmodels_account` MODIFY `project_token` varchar(255);
ALTER TABLE `pnmodels_account` MODIFY `url` varchar(255);
COMMIT;
I think MODIFY
0002 should be included NOT NULL
. Is this a django bug or am I missing something?
Usage: Django == 1.7.7, mysqlclient == 1.3.6
+3
source to share
1 answer
This is a known issue, see https://code.djangoproject.com/ticket/24595
While waiting for a fix, you may need to do it "manually" or make your column "null" and then invalid.
+2
source to share