Unique and null

31 views
Skip to first unread message

Mike Dewhirst

unread,
Apr 7, 2021, 2:07:35 AM4/7/21
to Django users
I have just made a CharField unique and Django detects duplicates nicely
but seems to ignore multiple instances where the field value is null.

    arn = models.CharField(
        max_length=MEDIUM,
        unique=True,
        null=True,
        blank=True,
    )

Is this correct behaviour?

Thanks

Mike



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

OpenPGP_signature

Derek

unread,
Apr 7, 2021, 9:05:34 AM4/7/21
to Django users

Ryan Nowakowski

unread,
Apr 7, 2021, 9:05:52 AM4/7/21
to django...@googlegroups.com
I'm not sure about the case with null. However, I believe that most uses of CharField leave null=False and use the empty string instead to represent "empty" value.

Vkash Poudel

unread,
Apr 7, 2021, 10:01:54 AM4/7/21
to django...@googlegroups.com
I'm using Postgresql 
does CharField unique solves problem of uniqueness ,
can we work with uuid model.
 

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/efd40f22-50d8-4250-9738-95d2152e68ecn%40googlegroups.com.


--
Best Regards,
Bikash Poudel

Roger Gammans

unread,
Apr 7, 2021, 10:26:58 AM4/7/21
to django...@googlegroups.com
Hi All,

Let shed some actually light on the whole problem, before people start piling in.

This is mostly (form the Django perspective) and issue with CharFields, because a CharField can store None or ''. For values of '' in CharField, IIRC, django will convert them to null, with null=True, blank=True. But not with blank=True,null=False.

So if it stores '', (with null=False) then the uniqueness should work as expected.

It is should (rather than will), because Django relies on the backend Database to enforce the constraints, so will depend on the level of conformance the Backend Db has.

Now SQL Specifies that two variables (or a literal and a field) which are None will ALWAYS compare as non-equal, and this stackoverflow ( https://stackoverflow.com/questions/767657/how-do-i-create-a-unique-constraint-that-also-allows-nulls/767702#767702 ) states the same concept holds for uniqeness .

SQLite, MySQL, Postgres all work, like this, but SQL Server apparently doesn't.

There's quite a bit there; but I hope it helps and isn't too confusing.

Roger Gammans

unread,
Apr 7, 2021, 10:28:55 AM4/7/21
to django...@googlegroups.com
I should have said SQL specifies NULL compares Non-equal, the ORM converts None's in python to NULL in SQL for you, as well as all the other things it does

Mike Dewhirst

unread,
Apr 7, 2021, 6:52:22 PM4/7/21
to django...@googlegroups.com
No. PostgreSQL.

I checked that post. And googled the answer. Postgres follows the SQL standard and permits multiple nulls in a unique column.

I should have done that instead of asking. I had first thought of it being a Django thing and couldn't find the answer in the docs.

Thanks for responding.

Cheers

Mike



--
(Unsigned mail from my phone)

Ing.Daniel Bojorge

unread,
Apr 7, 2021, 7:00:33 PM4/7/21
to django...@googlegroups.com
I suggest that if you set unique, the field must have value, so null=False.

Blank=True es for form and template, to set required.  Null es for database.



Mi Blog
Nicaragua

"Si ustedes permanecen unidos a mí, y si permanecen fieles a mis enseñanzas, pidan lo que quieran y se les dará.
(Juan 15:7 DHH)
Bendito el varón que se fía en el SEÑOR, y cuya confianza es el SEÑOR.
(Jeremías 17:7 RV2000)




Mike Dewhirst

unread,
Apr 7, 2021, 8:39:39 PM4/7/21
to django...@googlegroups.com

Ryan Nowakowski

unread,
Apr 8, 2021, 5:37:27 PM4/8/21
to django...@googlegroups.com
On Wed, Apr 07, 2021 at 03:25:53PM +0100, Roger Gammans wrote:
> This is mostly (form the Django perspective) and issue with CharFields,
> because a CharField can store None or ''. For values of ''
> in CharField, IIRC, django will convert them to null, with null=True,
> blank=True. But not with blank=True,null=False.See also :
> https://docs.djangoproject.com/en/3.1/ref/models/fields/#null

I read the documentation a bit differently. My interpretation is that
CharField(null=True) means that both ''(empty string) and None(null) are
valid values. But no implicit conversion from '' to None takes place.

CharField(null=False) means that None(null) isn't a valid value and that
only ''(empty string) is permitted.

Roger Gammans

unread,
Apr 8, 2021, 6:13:57 PM4/8/21
to django...@googlegroups.com
I understand that reading, and the simple answer is we are both right!

See:
https://github.com/django/django/blob/45a58c31e64dbfdecab1178b1d00a3803a90ea2d/django/db/models/fields/__init__.py#L1082

This shows the model field telling the any auto creating form field,
say like in the admin model, which value to prefer 'empty' values.

Since it's not the model field, so any python code which saves '' and
None, directed onto the model will still have those respected as '' and
NULL.

Ryan Nowakowski

unread,
Apr 8, 2021, 6:44:12 PM4/8/21
to django...@googlegroups.com
Whhhhhyyyyyyyy Oracle!!!!!!! *sigh* :)


find django/db/backends -name "features.py" | xargs grep interprets_empty_strings_as_nulls
django/db/backends/oracle/features.py: interprets_empty_strings_as_nulls = True
django/db/backends/base/features.py: interprets_empty_strings_as_nulls = False
Reply all
Reply to author
Forward
0 new messages