I believe I found a limitation in the Django jsonb support, but I'm really 
not sure how the framework could fix it beside adding a limitation section 
in the documentation.

Basically, it is possible to fetch models with json 'null' values but it is 
not possible to save them back (Django converts the json 'null' value to 
sql NULL value).

Assume the following model:

class Container(models.Model):
    data = JSONField(blank=False, null=False) # emphasis on null=False


In PostgreSQL, it is perfectly reasonable to do this:

INSERT INTO container VALUES (1, 'null');


If you fetch the row in Django, you will get this:

container = Container.objects.get(pk=1)

assert container.data is None # passes

container.save() # raises IntegrityError because it tries to save NULL in a 
NOT NULL column.


This is because Django has no idea whether you want to store None or 
'null'::jsonb.


I'm not sure if this limitation was discussed elsewhere. I found a 
somehow-related ticket here: https://code.djangoproject.com/ticket/25718.


Barthélémy

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/cf59a2a5-2472-4091-ac87-180884e0c076%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to