2023年8月3日发(作者:)
mysqlupdatejson字段,更新MysqlJSON字段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 SQLstatement: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'.解决⽅案My code works now as intended.
2023年8月3日发(作者:)
mysqlupdatejson字段,更新MysqlJSON字段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 SQLstatement: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'.解决⽅案My code works now as intended.
发布评论