Django: 3.2.4
Python: 3.8.7
MySQL: 8.0.19
OS: CentOS 7
----
Referenced:
Ticket: https://code.djangoproject.com/ticket/32503
PR: https://github.com/django/django/pull/14421
Commit:
https://github.com/django/django/pull/14421/commits/5e04e84d67da8163f365e9f5fcd169e2630e2873
----
New project used for reproducing the error:
Project: https://github.com/omkar-dsd/almanac
----
Error:
{{{
django.db.utils.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON
column 'new_txt' can't have a default value")
}}}
Occurred on migration:
{{{
migrations.AddField(
model_name='nucleusdata',
name='new_txt',
field=models.TextField(blank=True, default='abc', null=True),
)
}}}
----
How to reproduce:
1. Add new TextField or JSONField with default on pre-existing model, with
Django < 3.2
2. Upgrade Django version to 3.2.4
3. Run migration which involves creation of of the new field added in step
1.
----
Details:
This occurs for all JSON and Text Fields with default value.
This occurred to me when I upgraded from Django 2.2.24 to 3.2.4 and ran
migrations on fresh DB.
This error did not occur when upgraded from Django 2.2.24 to 3.1.12. And,
it occurs for all 3.2.X till date.
New project was created to demonstrate the issue it available on the above
GitHub link for reference.
--
Ticket URL: <https://code.djangoproject.com/ticket/32832>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "requirements.txt" added.
Python Project Requirement
* Attachment "models.py" added.
models.py
* Attachment "0001_initial.py" added.
Initial Migration to Create the Table
* Attachment "0002_auto_20210609_1553.py" added.
Second Migration to Alter the fields
* Attachment "0003_nucleusdata_new_txt.py" added.
Third Migration to AddField with default value on which migration breaks
* Attachment "django_AddField_error.txt" added.
Full Error Traceback
* cc: Simon Charette, Jordan Bae (added)
* severity: Normal => Release blocker
Comment:
Regression in d4ac23bee1c84d8e4610350202ac068fc90f38c0.
Reproduced at fcd44b889f36c4be87910745614a0a4c88d7a3d8.
We can consider backporting 5e04e84d67da8163f365e9f5fcd169e2630e2873 and
fix it with:
{{{
diff --git a/django/db/backends/base/schema.py
b/django/db/backends/base/schema.py
index c409464eca..77c5ce1629 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -900,7 +900,7 @@ class BaseDatabaseSchemaEditor:
new_db_params =
new_field.db_parameters(connection=self.connection)
if drop:
- if new_field.null:
+ if new_field.null and not
self.skip_default_on_alter(new_field):
sql = self.sql_alter_column_no_default_null
else:
sql = self.sql_alter_column_no_default
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32832#comment:1>
Comment (by Mariusz Felisiak):
Omkar, thanks for the detailed report!
--
Ticket URL: <https://code.djangoproject.com/ticket/32832#comment:2>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/32832#comment:3>
* owner: nobody => Mariusz Felisiak
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32832#comment:4>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/14512 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/32832#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"fa0433d05f213afe4c67055006320f7aba4c8108" fa0433d0]:
{{{
#!CommitTicketReference repository=""
revision="fa0433d05f213afe4c67055006320f7aba4c8108"
Fixed #32832 -- Fixed adding BLOB/TEXT nullable field with default on
MySQL 8.0.13+.
Regression in d4ac23bee1c84d8e4610350202ac068fc90f38c0.
Thanks Omkar Deshpande for the report.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32832#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"826a1659eed0b23f5b814d5a293eeaca20d29a6c" 826a1659]:
{{{
#!CommitTicketReference repository=""
revision="826a1659eed0b23f5b814d5a293eeaca20d29a6c"
[3.2.x] Fixed #32832 -- Fixed adding BLOB/TEXT nullable field with default
on MySQL 8.0.13+.
Regression in d4ac23bee1c84d8e4610350202ac068fc90f38c0.
Thanks Omkar Deshpande for the report.
Backport of fa0433d05f213afe4c67055006320f7aba4c8108 from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32832#comment:7>