3YOURMIND / django-add-default-value

This django Migration Operation can be used to transfer a Fields default value to the database scheme.
Apache License 2.0
138 stars 19 forks source link

Using `None` as a default value on mysql #23

Open sobolevn opened 3 years ago

sobolevn commented 3 years ago

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

David-Wobrock commented 3 years ago

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