django-add-default-value icon indicating copy to clipboard operation
django-add-default-value copied to clipboard

Using `None` as a default value on mysql

Open sobolevn opened this issue 5 years ago • 1 comments

Hi! Thanks a lot for this library. I am trying to use it like this:

AddDefaultValue(
            model_name='mymodel',
            name='weight',
            value=None,
        ),

And this happens:

.venv/lib/python3.7/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1067, "Invalid default value for 'weight'")

It is unclear from the docs how to use None

sobolevn avatar Sep 29 '20 09:09 sobolevn

Hi @sobolevn Thanks for submitting an issue :)

I do not think that it is necessary to have a AddDefaultValue operation for a default NULL value. On MySQL, a Python None is a SQL NULL.

Either the weight column is nullable, and the default value will be NULL by convention (when not specifying the value in an INSERT => is null by default, or Django will define it in an INSERT => Django will put a null) => you don't need a AddDefaultValue Or the column is NOT NULL, in which case the default value cannot be NULL anyway

David-Wobrock avatar Oct 02 '20 09:10 David-Wobrock