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

MySQL CURRENT_TIMESTAMP require extra parentheses #40

Open todorvelichkov opened 1 year ago

todorvelichkov commented 1 year ago

When using the undocumented NOWlike so:

from django_add_default_value import AddDefaultValue, NOW

# ....

class Migration(migrations.Migration):
    operations = [
        # ...
        AddDefaultValue(model_name="mymodel", name="updated_at", value=NOW)
        # ....
    ]

Generates the following SQL:

ALTER TABLE `app_mymodel` ALTER COLUMN `updated_at` SET DEFAULT CURRENT_TIMESTAMP;

However on MySQL 8.0.33, this triggers the following error (I believe this behaviour starts from 8.0.13, but I'm not 100% sure):

django.db.utils.ProgrammingError: (1064, "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 'CURRENT_TIMESTAMP' at line 1")

The correct syntax is with extra parentheses around CURRENT_TIMESTAMP like so:

ALTER TABLE `app_mymodel` ALTER COLUMN `updated_at` SET DEFAULT (CURRENT_TIMESTAMP);