【问题标题】:Updating Mysql JSON field更新 Mysql JSON 字段
【发布时间】:2017-06-11 15:30:02
【问题描述】:

我正在使用 django 1.8.9 和 MySQL 5.7.17。我有一个下表:

+----------+--------------+------+-----+---------+----------------+
| 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):

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, 'error fully whack')。无论如何,它最终会出现在以下 SQL 语句中:

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

或多或少的预期结果:

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

简单的搜索给出了这个结果:https://code.djangoproject.com/ticket/26140,所以也许这是正确的做法,我的 mysql(或 django)配置错误。有没有人知道如何解决它?

【问题讨论】:

  • jango 在 1.8 中原生不支持任何类型的 json 字段,即使在 1.10 中也不支持 mysql json
  • @e4c5,“本机”是什么意思?他们声称他们有,他们有这个字段(django-mysql.readthedocs.io/en/latest/model_fields/…),代码(在 atm 中查看:))。
  • 这是一个第三方插件。如果那是你正在使用的。更新您的问题以表明这一事实。
  • @Sayse,如果我理解正确,我使用 django_mysql(来自 django_mysql.models 导入 JSONField)。对不起,如果我不这样做,我使用 django 大约一个星期。

标签: django django-models django-mysql


【解决方案1】:

事实证明,最近https://github.com/PyMySQL/mysqlclient-python 中的 _binary 前缀周围有些激动(如果不是大惊小怪的话)。所以我使用的版本 1.3.8 受到了它的影响,而在 1.3.9 中,更改已被恢复。 我的代码现在按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    相关资源
    最近更新 更多