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.See also : 
https://docs.djangoproject.com/en/3.1/ref/models/fields/#null
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.
On Wed, 2021-04-07 at 06:05 -0700, Derek wrote:
> Are you using MySQL?
> https://stackoverflow.com/questions/3712222/does-mysql-ignore-null-values-on-unique-constraints
> 
> 
> On Wednesday, 7 April 2021 at 08:07:35 UTC+2 Mike Dewhirst wrote:
> > 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
> > 
> > 
> > 
> > 
> > 
> > 
> > 

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2b5b6c7e1fe015bb1e8c4f28ded77e70cd3ae988.camel%40gammascience.co.uk.

Reply via email to