0

I'm using django 1.8.9 and MySQL 5.7.17. I've a following table:

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| brand    | varchar(255) | YES  | MUL | NULL    |                |
| new_info | json         | YES  |     | NULL    |                |
| keywords | json         | YES  |     | NULL    |                |
| notes    | varchar(255) | YES  |     | NULL    |                |
| id       | mediumint(9) | NO   | MUL | NULL    | auto_increment |
+----------+--------------+------+-----+---------+----------------+

In django I have this model (JSONField is from django_mysql):

class info_by_kw(Model):
    brand = ForeignKey(Brand, on_delete=CASCADE, db_column='brand')
    # new_info = JSONField(blank=True, null=True)
    keywords = JSONField(blank=True, null=True)
    notes = CharField(max_length=255, blank=True, null=True)

When the data is saved I get a very meaningful error: (-1, 'error totally whack'). Anyway, it ends up in the following SQL statement:

UPDATE `products_info_by_kw` SET `brand` = _binary'BINGO!', `keywords` = _binary'[[\\"Tomato\\", \\"Madness\\"]]', `notes` = _binary'' WHERE `products_info_by_kw`.`id` = 48;

With the more or less expected result:

ERROR 3144 (22032): Cannot create a JSON value from a string with CHARACTER SET 'binary'.

Brief search gave this result: https://code.djangoproject.com/ticket/26140, so maybe it's a right thing to do and my mysql (or django) is misconfigured. Does anyone have an idea of how to fix it?

Sayse
  • 42,633
  • 14
  • 77
  • 146
aikipooh
  • 137
  • 1
  • 19
  • jango does't support any sort of json field natively in 1.8 and doesn't support mysql json even in 1.10 – e4c5 Jan 26 '17 at 07:40
  • @e4c5, what do you mean by “natively”? They claim they do, they have this field (http://django-mysql.readthedocs.io/en/latest/model_fields/json_field.html), the code (looking into it atm:)). – aikipooh Jan 26 '17 at 07:49
  • that's a third party plugin. If that's what you are using. update your question to indicate that fact. – e4c5 Jan 26 '17 at 07:54
  • @Sayse, if I understand correctly I use django_mysql (from django_mysql.models import JSONField). Sorry if I don't, I'm using django for about a week. – aikipooh Jan 26 '17 at 08:06

1 Answers1

0

Turned out that there was some agitation (if not fuss) around the _binary prefix lately in https://github.com/PyMySQL/mysqlclient-python. So version 1.3.8 that I was using was affected by it, while in 1.3.9 the change has been reverted. My code works now as intended.

aikipooh
  • 137
  • 1
  • 19