sqlmigrate does not quote default string values

51 views
Skip to first unread message

Michael Grijalva

unread,
Feb 1, 2017, 8:11:21 PM2/1/17
to Django users
Not sure if this is considered a bug, but the SQL output from sqlmigrate contains a small syntax error with default string values:

Add a new column as so:
class City(models.Model):
   
...
    name
= models.CharField(max_length=100, default='a b c d')

Create migration, and run sqlmigrate command:
BEGIN;
--
-- Add field name to city
--
ALTER TABLE
`map_city` ADD COLUMN `name ` varchar(100) DEFAULT a b c d NOT NULL;
ALTER TABLE
`map_city` ALTER COLUMN `name ` DROP DEFAULT;
COMMIT
;

Notice 'a b c d' is missing quotes. When this migration is actually applied it runs through cursor.execute, which properly quotes the value.

I realize sqlmigrate is only for getting a preview of the SQL, but other parts like the table name and columns are correctly quoted, so why not the default?

Tim Graham

unread,
Feb 2, 2017, 10:57:16 AM2/2/17
to Django users
It looks like a bug at first glance. I encourage you to look at Django's source code and try to confirm and fix it.

Michael Grijalva

unread,
Feb 3, 2017, 1:27:31 PM2/3/17
to Django users
Ok thank you. I will dig into it more.

Markus Holtermann

unread,
Feb 3, 2017, 4:14:14 PM2/3/17
to Django users
I gave it a quick look, but I can't reproduce this issue on the current master branch for SQLite3, PostgreSQL or MySQL. Can you give us more information when you report the issue please, such as Django version, database + version, database driver + version, and anything you think might be helpful. Thank you

/Markus

Michael Grijalva

unread,
Feb 6, 2017, 7:18:33 PM2/6/17
to Django users
Finally had some time to check again. I initially noticed this issue with Django 1.7 + MySQL, but I retested with the latest version of Django:

Django==1.10.5 (latest version from pip)
MySQL-python==1.2.5

On Python2.7.13 with MySQL 5.7.10 (installed via homebrew). I also tried mysqlclient (1.3.9), same issue. I dug into the Django source, and I see the value going through quote_value here: https://github.com/django/django/blob/master/django/db/backends/mysql/schema.py#L30

I believe I found the issue:
>>> import MySQLdb.converters
>>> print MySQLdb.escape('a b c d', MySQLdb.converters.conversions)
'a b c d'
>>> print MySQLdb.escape(u'a b c d', MySQLdb.converters.conversions)
a b c d

Inside quote_value, Django passes a unicode object for the value arg, and MySQLdb fails to quote it properly. With this, it appears to be a bug with mysql-python itself, or my mysql version?
Reply all
Reply to author
Forward
0 new messages