As I understand from this database design question, you should use nullable fields sparingly and make a weighed decision each time on whether it would be better to rethink data design instead.
But let's say in some particular case the resolution is to allow a text field to contain an empty value. Say, there's user
table and there's short_bio
column that is represented by the TextField in Django and is not mandatory. It doesn't make sense to create a separate user_bio
table, right?
Now the question is, should the empty value be an empty string or null marker? What are pros and cons for each option? Are there any specifics in how Django works with database that can make difference?
It should be noted that django-lint currently reports on CharField and TextField instances with null=True
.
Conversely, ‘storing an empty string for a field left blank is seen as a Bad Idea’ by some developers.
I think that when 'empty string' means 'no value', the empty string should be used in order to avoid mixing nulls and blanks meaning the same thing (e.g. in searching or comparing: in some DBMS you can't compare NULL to NULL directly, you must use the IS NULL
operator).
Using the empty string will also make easier to use it 'as is' in reports and other stuffs where the 'short_bio' is used without passing throug the DOM (e.g. using objects.values('short_bio')
), without converting Null in ''.
Moreover, Django docs state:
Avoid using null on string-based fields such as CharField and TextField unless you have an excellent reason. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” Django convention is to use the empty string, not NULL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With