JSONField default passed to SQL create column query
See original GitHub issueSummary: Migrations created to add a JSONField include default, causing query to fail.
Django Version: Django 1.9.2-8
Database and version used: MySQL 5.7.11
Version: Django-MySQL 1.0.1
Hi,
I’ve been having some inconsistent behaviour with this so sorry if this is confusing. In the current situation I’m stuck in, I have model that was created with a JSONField without any problems, and I’m trying to add another JSONField. It keeps failing because the migrations tries to set a default on the field, which MySQL doesn’t allow:
(django.db.utils.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'tables' can't have a default value")
)
I think I know why this is happening: https://github.com/django/django/blob/6bf7964023487f2a352084e74aca27aecb354d6c/django/db/backends/mysql/schema.py#L28
That method tells migrations to leave out the default for certain fields, and I believe JSONField would now need to be included among those, as the django_mysql JSONField implicitly sets dict
as its default.
I can’t immediately see how this could be rectified here. Do you think it requires a bug report for Django? Or maybe I’ve overlooked something.
I know I’ve left out full tracebacks and stuff but that’s just because I’m pretty sure I’ve located the issue above. Let me know if you’d like more.
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (9 by maintainers)
Top GitHub Comments
You can set
Default
value toNone
:Ok I’ve made a PR against Django at django/django#6997, closing this since that will resolve it. If you need it before Django < 1.11 it’s possible to fix it with your own database backend by subclassing the existing one, its
SchemaEditor
class, and including the fixedskip_default
method. I can help you with this if you need it!