Re: Nullable vs empty strings

2018-05-03 Thread Ken Whitesell
Nick,

 A null string (string with length 0) is _not_ the same as a null field 
(no string). The two are distinct, and (can) serve two very different 
functions. 

Take a look at this for some more detailed 
information: 
https://softwareengineering.stackexchange.com/questions/32578/sql-empty-string-vs-null-value

Ken


On Thursday, May 3, 2018 at 5:17:35 AM UTC-4, Nick Sarbicki wrote:
>
> I just saw this in the docs: 
> https://docs.djangoproject.com/en/2.0/ref/models/fields/#null
>
> Suggesting that you should never set a CharField to null unless using a 
> unique index.
>
> Is this generally accepted? Historically I've always nulled a CharField 
> because using empty strings, as opposed to letting a column be nullable, 
> has felt very dirty to me.
>
> I would have expected (not suggesting this as a change) an empty string to 
> fail if the field isn't nullable.
>
> Am I on my own here?
>

-- 
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/23e3a4dc-345a-4fb9-8122-0dba7b99bb4d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Nullable vs empty strings

2018-05-03 Thread Jani Tiainen
And then you find out that Oracle implicitly converts empty strings to
NULLs which causes all kind of hassle. :)


On Thu, May 3, 2018 at 3:39 PM, Ken Whitesell 
wrote:

> Nick,
>
>  A null string (string with length 0) is _not_ the same as a null
> field (no string). The two are distinct, and (can) serve two very different
> functions.
>
> Take a look at this for some more detailed information: https://
> softwareengineering.stackexchange.com/questions/32578/sql-empty-string-vs-
> null-value
>
> Ken
>
>
> On Thursday, May 3, 2018 at 5:17:35 AM UTC-4, Nick Sarbicki wrote:
>>
>> I just saw this in the docs: https://docs.djangoproje
>> ct.com/en/2.0/ref/models/fields/#null
>>
>> Suggesting that you should never set a CharField to null unless using a
>> unique index.
>>
>> Is this generally accepted? Historically I've always nulled a CharField
>> because using empty strings, as opposed to letting a column be nullable,
>> has felt very dirty to me.
>>
>> I would have expected (not suggesting this as a change) an empty string
>> to fail if the field isn't nullable.
>>
>> Am I on my own here?
>>
> --
> 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/23e3a4dc-345a-4fb9-8122-0dba7b99bb4d%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
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/CAHn91of_PjMgOqpOHhN4q572qLDX%2Bi_SDBrNuZpBthUDyn6r5w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Nullable vs empty strings

2018-05-03 Thread Nick Sarbicki
Thanks for the replies both.

I know the difference between a NULL value and an empty string :-D. My 
question was more why Django recommends _never_ using null=True on 
CharFields.

>From both replies it sounds like neither of you agree with this guideline...

On Thursday, May 3, 2018 at 2:18:32 PM UTC+1, Jani Tiainen wrote:
>
> And then you find out that Oracle implicitly converts empty strings to 
> NULLs which causes all kind of hassle. :)
>
>
> On Thu, May 3, 2018 at 3:39 PM, Ken Whitesell  > wrote:
>
>> Nick,
>>
>>  A null string (string with length 0) is _not_ the same as a null 
>> field (no string). The two are distinct, and (can) serve two very different 
>> functions. 
>>
>> Take a look at this for some more detailed information: 
>> https://softwareengineering.stackexchange.com/questions/32578/sql-empty-string-vs-null-value
>>
>> Ken
>>
>>
>> On Thursday, May 3, 2018 at 5:17:35 AM UTC-4, Nick Sarbicki wrote:
>>>
>>> I just saw this in the docs: 
>>> https://docs.djangoproject.com/en/2.0/ref/models/fields/#null
>>>
>>> Suggesting that you should never set a CharField to null unless using a 
>>> unique index.
>>>
>>> Is this generally accepted? Historically I've always nulled a CharField 
>>> because using empty strings, as opposed to letting a column be nullable, 
>>> has felt very dirty to me.
>>>
>>> I would have expected (not suggesting this as a change) an empty string 
>>> to fail if the field isn't nullable.
>>>
>>> Am I on my own here?
>>>
>> -- 
>> 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...@googlegroups.com .
>> To post to this group, send email to django...@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/23e3a4dc-345a-4fb9-8122-0dba7b99bb4d%40googlegroups.com
>>  
>> 
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Jani Tiainen
>
> - Well planned is half done, and a half done has been sufficient before...
>

-- 
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/c445d4ab-abf6-4cd6-9123-f9f2d0d8900d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Nullable vs empty strings

2018-05-03 Thread George Lubaretsi
Short answer: because you'll know when you need it. And unless that's the
case - there's no good reason to do it.

Long answer:

Because there's really no reason to do it except for when you have unique
constraint on that column. It's just a convention. When you have a
`CharField`, you expect returned type to be a string and do string
operations on it. But then you allow null values and suddenly you get
NoneType instead of string. This convention just means that you don't need
it, so don't use it.

The only time where you need nullable text columns, is when you need to
allow for empty values (or unknown values) in the DB, but also need to have
a unique constraint on that column.

Imagine where you have a model that stores phone number in a text field.
That field is not required, so it can be empty, but at the same time, if
it's provided, it must be unique. That's the only time where you need to
set `null=True`, because every `null` has unique, distinct value in DB
(empty string does not). So you'll make that column not required, but at
the same time, enforce uniqueness. And because you know that this field is
optional and may be missing, you'll be explicitly checking for `None`
values before you do any string operations on it. But if you don't need
phone numbers to be unique, you'll be checking for `None` values
explicitly, when simply an empty string will be enough.

On Thu, May 3, 2018 at 11:37 PM Nick Sarbicki 
wrote:

> Thanks for the replies both.
>
> I know the difference between a NULL value and an empty string :-D. My
> question was more why Django recommends _never_ using null=True on
> CharFields.
>
> From both replies it sounds like neither of you agree with this
> guideline...
>
> On Thursday, May 3, 2018 at 2:18:32 PM UTC+1, Jani Tiainen wrote:
>
>> And then you find out that Oracle implicitly converts empty strings to
>> NULLs which causes all kind of hassle. :)
>>
>> On Thu, May 3, 2018 at 3:39 PM, Ken Whitesell 
>> wrote:
>>
> Nick,
>>>
>>>  A null string (string with length 0) is _not_ the same as a null
>>> field (no string). The two are distinct, and (can) serve two very different
>>> functions.
>>>
>>> Take a look at this for some more detailed information:
>>> https://softwareengineering.stackexchange.com/questions/32578/sql-empty-string-vs-null-value
>>>
>>> Ken
>>>
>>>
>>> On Thursday, May 3, 2018 at 5:17:35 AM UTC-4, Nick Sarbicki wrote:

 I just saw this in the docs:
 https://docs.djangoproject.com/en/2.0/ref/models/fields/#null

 Suggesting that you should never set a CharField to null unless using a
 unique index.

 Is this generally accepted? Historically I've always nulled a CharField
 because using empty strings, as opposed to letting a column be nullable,
 has felt very dirty to me.

 I would have expected (not suggesting this as a change) an empty string
 to fail if the field isn't nullable.

 Am I on my own here?

>>> --
>>> 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...@googlegroups.com.
>>> To post to this group, send email to django...@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/23e3a4dc-345a-4fb9-8122-0dba7b99bb4d%40googlegroups.com
>>> 
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> Jani Tiainen
>>
>> - Well planned is half done, and a half done has been sufficient before...
>>
> --
> 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/c445d4ab-abf6-4cd6-9123-f9f2d0d8900d%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.googl