question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSONField default passed to SQL create column query

See original GitHub issue

Summary: 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:closed
  • Created 7 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
iosifnicolae2commented, Oct 6, 2017

You can set Default value to None: image

1reaction
adamchainzcommented, Aug 1, 2016

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 fixed skip_default method. I can help you with this if you need it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mysql set default value to a json type column - Stack Overflow
The BLOB , TEXT , GEOMETRY , and JSON data types can be assigned a default value only if the value is written...
Read more >
How to Use JSON Data Fields in MySQL Databases - SitePoint
Note that JSON columns can't have a default value, be used as a primary key, be used as a foreign key, or have...
Read more >
Modifying JSON data using JSON_MODIFY() in SQL Server
We use JSON_MODIFY() function to update the JSON string. It can update the following items: Update existing property value; Add a new element...
Read more >
How to Query a JSON Column in PostgreSQL - PopSQL
Here's how you can query your JSON column in PostgreSQL: -- Give me params.name (text) from the ... Get more done, together, with...
Read more >
postgresql: how to define a JSONB column with default value
That's the same as with any other default value: create table amsterdam ( id integer primary key, payload jsonb not null default '{}'::jsonb...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found