更新Mysql JSON字段 [英] Updating Mysql JSON field
问题描述
我正在使用Django 1.8.9和MySQL 5.7.17。我有一张下表:
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 |
+----------+--------------+------+-----+---------+----------------+
在Django中,我有这个模型( JSONField
来自django_mysql):
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)
当数据为保存,我得到一个非常有意义的错误:(-1,错误完全重击)。无论如何,它最终会出现在以下SQL语句中:
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'.
简短搜索给出了以下结果: https://code.djangoproject.com/ticket/26140 ,所以这样做是对的,我的mysql(或django)配置错误。有没有人知道如何解决它?
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?
推荐答案
原来,周围有一些激动(如果不是大惊小怪) _binary前缀最近出现在 https://github.com/PyMySQL/mysqlclient-python 中。因此,我正在使用的1.3.8版本受它的影响,而在1.3.9中,更改已恢复。
我的代码现在可以按预期工作了。
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.
这篇关于更新Mysql JSON字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!