django-mysql icon indicating copy to clipboard operation
django-mysql copied to clipboard

Test MariaDB 10.2, activate JSONField for it

Open adamchainz opened this issue 8 years ago • 23 comments

Summary: It looks like MariaDB 10.2 supports the JSON type ( https://mariadb.com/kb/en/mariadb/json-functions/ ) and also more goodness ( https://mariadb.com/kb/en/mariadb/what-is-mariadb-102/ ). We should start testing on it, and check if JSONField can work on it (its system check + docs will then need updating).

adamchainz avatar Dec 22 '16 23:12 adamchainz

Any update on this issue?

dekelb avatar Jul 02 '17 17:07 dekelb

I've had a brief look. To be truly compatible we'd need to also add a check to the column, like CHECK JSON_VALID(fieldname), so that inserting invalid JSON isn't possible - this is what the MariaDB docs advise. Django doesn't support constraints like this at the moment, let alone them coming from fields, but it's worth looking at before doing this.

If you want to add it TODAY and don't mind adding the CHECK yourself, you could subclass the built-in JSONField and disable the check that we're on Oracle MySQL 5.7, that should be enough to let you use it.

adamchainz avatar Jul 02 '17 17:07 adamchainz

What about adding a trigger on INSERT for the CHECK JSON_VALID?

dekelb avatar Jul 02 '17 18:07 dekelb

Triggers aren't quite the same as CHECK constraints, see https://mariadb.com/kb/en/mariadb/constraint/#check-constraint-expressions . Neither triggers nor CHECK constraints have any code representation in Django which is what makes them challenging to add.

adamchainz avatar Jul 02 '17 18:07 adamchainz

@adamchainz I would like to use django-mysql with MariaDB 10.2+ and query JSON fields, is it currently possible? thank you.

afausti avatar Oct 23 '17 21:10 afausti

@afausti I'm afraid not, as otherwise this ticket would have been closed :)

adamchainz avatar Oct 24 '17 09:10 adamchainz

So I've just realized I've confused column checks and table checks. Django already supports column checks - retrieved from https://github.com/django/django/blob/master/django/db/models/fields/init.py#L667 - and seems to apply them even on backends where supports_column_check_constraints is False (like the MySQL one, for now). So we can go ahead and use them :)

MariaDB 10.2 also supports json as a data type, but only as an alias for text. So we'll add the json_valid check constraint for both MySQL (where it will be parsed but not applied) and MariaDB.

adamchainz avatar Nov 06 '17 18:11 adamchainz

@adamchainz I also would like to use the JSON fields in our application (we are running Django 1.9.2 with MariaDB 10.2.10). Whenever I try to use it, I get the following error during migrations:

SystemCheckError:

System check identified some issues:

ERRORS: controller.Device.properties: (django_mysql.E016) MySQL 5.7+ is required to use JSONField

Is this because the django-mysql package only checks MySQL version > 5.7?

Kind regards,

Diederik

diedoman avatar Nov 23 '17 11:11 diedoman

@diedoman until this issue is closed, JSONField is only supported on MySQL 5.7. You can always use SILENCED_SYSTEM_CHECKS and use it in 'unsupported' mode - it won't be enforcing JSON is valid at the DB level. Or you can use DynamicField.

adamchainz avatar Nov 23 '17 18:11 adamchainz

So I've tried the 'unsupported' mode with MariaDB 10.2.15. I've also tested adding the json_valid check constraint (using db_check method) and it mostly worked. There is a problem with path lookups though. The SQL produced contains CAST(%s AS JSON) and MariaDB doesn't like it (anymore? https://jira.mariadb.org/browse/MDEV-11439).

Apart from sharing my experience, I think I'd like to ask if there are any plans to enable JSONField for MariaDB in a 'supported' mode? ;)

iwaszko avatar May 20 '18 20:05 iwaszko

I started a branch to work on this back in November-ish, I found the same problem you're reporting. I will have another look at Djangocon EU this week.

adamchainz avatar May 21 '18 09:05 adamchainz

Great! Thanks for a hint about the new branch.

iwaszko avatar May 21 '18 14:05 iwaszko

Is there any update on this?

yvesonline avatar Aug 23 '18 09:08 yvesonline

Afraid not, if there was it would be here.

adamchainz avatar Aug 23 '18 09:08 adamchainz

Alright, thanks for the quick response @adamchainz !

yvesonline avatar Aug 23 '18 14:08 yvesonline

Is there any update? I need to use JsonField() with MariaDB 10.4.0 and Django 2.1.7.

safuente avatar May 17 '19 10:05 safuente

There's no update, again if there was it would be here. I have limited time to work on open source. From my initial experimetns, I recall this could require a good few hours to tackle. If you want to sponsor development of this feature I'm open to that - email [email protected] for more details.

In other news there's a Google Summer of Code student working on Django to bring a JSONField to core: https://summerofcode.withgoogle.com/organizations/5465143218012160/#projects / https://code.djangoproject.com/wiki/SummerOfCode2019 . This will bring in some code from this library and if all goes well it will be available in Django 3.0.

adamchainz avatar May 19 '19 11:05 adamchainz

Unfortunately, Django 3.0 has no support for MySQL/MariaDB specific fields.

What's your vision about this now? :)

barseghyanartur avatar Dec 03 '19 09:12 barseghyanartur

I don't have time to work on this right now (at least unpaid).

My main vision would be to spend time getting the universal JSONField merged to Django core. After that's done, I'd be open to making django-jsonfield, or perhaps a new package with a new name, a copy of that universal field backported to older Django versions.

adamchainz avatar Dec 13 '19 19:12 adamchainz

So I've tried the 'unsupported' mode with MariaDB 10.2.15. I've also tested adding the json_valid check constraint (using db_check method) and it mostly worked. There is a problem with path lookups though. The SQL produced contains CAST(%s AS JSON) and MariaDB doesn't like it (anymore? https://jira.mariadb.org/browse/MDEV-11439).

Apart from sharing my experience, I think I'd like to ask if there are any plans to enable JSONField for MariaDB in a 'supported' mode? ;)

I just hit this as well when working on a new app after choosing Django and MariaDB. IMHO the installation section (https://django-mysql.readthedocs.io/en/latest/installation.html) listing MariaDB as tested and supported is a bit misleading as one would expect it to works the same as MySQL, would be nice to at least add some precaution on using JSON column.

ronaldchoi avatar Jan 13 '20 05:01 ronaldchoi

@ronaldchoi The JSONField docs state:

This field requires Django 1.8+ and MySQL 5.7+.

I'd accept a PR adding a sentence to the installation instructions that not all features are supported/tested on all database versions.

adamchainz avatar Jan 13 '20 07:01 adamchainz

I also get this message: "MySQL 5.7+ is required to use JSONField".

My env: Python 3.7.7, Django 3.0.7, MariaDB 10.5.3, mysqlclient 2.0.0, django-mysql 3.7.1.

Any solution to create a json field in MariaDB?

JulianGarciaDev avatar Jul 02 '20 08:07 JulianGarciaDev

Django 3.1 in beta has a univresal JSONField, the dev who made it is working on a backport package: https://twitter.com/laymonage/status/1278485132765085696

adamchainz avatar Jul 02 '20 10:07 adamchainz