0

I'm adding a not null CharField to one of my models:

channel_class = models.CharField(max_length=10, blank=True)

I create the migration with:

django-admin.py makemigrations webapp

Which asks me nothing about default values (see this ticket about default values prompting for blank=True).

The SQL generated for this migration is:

$ django-admin.py sqlmigrate webapp 0003_auto_20150514_1112
BEGIN;
ALTER TABLE `webapp_lead` ADD COLUMN `channel_class` varchar(10) DEFAULT  NOT NULL;                                                                                                                                                                                            
ALTER TABLE `webapp_lead` ALTER COLUMN `channel_class` DROP DEFAULT; 
COMMIT;

However if I insert that into the MySQL editor that line I get an error:

mysql> ALTER TABLE `webapp_lead` ADD COLUMN `channel_class` varchar(10) DEFAULT  NOT NULL;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL' at line 1

It works if I use:

mysql> ALTER TABLE `webapp_lead` ADD COLUMN `channel_class` varchar(10) DEFAULT '' NOT NULL;

I.e. if I specify an empty string as default.

My guessing is that the fix for this bug introduced a new bug?

dukebody
  • 7,025
  • 3
  • 36
  • 61

1 Answers1

2

https://code.djangoproject.com/ticket/24803

It is a bug in django. In django.db.backends.base.schema.BaseDatabaseSchemaEditor#execute, when collected SQL is stored self.collected_sql, empty string is passed to formatted string it the wrong way, so resulted SQL comes without ''.

coldmind
  • 5,167
  • 2
  • 22
  • 22