0

With MySQL8.0.25 as database backend, I make Django ORM to update the following JSONField value

{
  "01-task1": {"data":"huge data here"}
}

just add a new element , "02-testing": {}

but I got the following sql log

(0.000) UPDATE `by_jsontree_store1` SET `store_type` = 1, `user_id` = 2, `data` = '{\"01-task1\": {\"data\": \"huge data here\"}, \"02-testing\": {}}' 

WHERE `by_jsontree_store1`.`id` = 1; args=(1, 2, '{"01-task1": {"data": "huge data here"}, "02-testing": {}}', 1)

It looks like Django rewrite the whole content of that JSONField.

The Django JSONField does map to MySQL JSON column type, as the following table struct shows.

enter image description here

Imaging if the original content is huge, like 100MB, what could happen.

Could Django update JSON field partially, just update what it need to?

Jcyrss
  • 1,513
  • 3
  • 19
  • 31
  • You can use chained lookups for key or index based transformations on `JSONField`, but I don't think such an operation is permitted by the `update` method. – Abhyudai Jul 20 '21 at 10:18
  • So you mean, for time being, Django can not convert ORM JSONField updating to to JSON_SET, JSON_REPLACE and JSON_REMOVE in MYSQL8.0 ? So we have to write raw sql to do that? – Jcyrss Jul 21 '21 at 00:55
  • 1
    if there's a way, maybe posting on forum.djangoproject.com can look. There are more django developers there. – Abhyudai Jul 21 '21 at 06:04
  • Thank you, I've posted. https://forum.djangoproject.com/t/how-to-make-django-modifiy-jsonfield-value-effictively/8937 – Jcyrss Jul 25 '21 at 12:23

0 Answers0