#27627: OneToOneField doesn't allow multiple records with NULL value on 
Microsoft
SQL Server
-----------------------------------------+------------------------
               Reporter:  Ryan Schave    |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 A OneToOneField with null=True should allow multiple records to be saved
 with a NULL value in the field.  Consider the following models:

 {{{
 class Company(models.Model):
     name = models.CharField(max_length=30)


 class UserProfile(models.Model):
     case_manager = models.BooleanField(default=False)
     company = models.OneToOneField(Company, models.SET_NULL, blank=True,
 null=True)
 }}}

 With a SQLite or PostgreSQL backend I'm able to save multiple records with
 a NULL value:

 {{{
 up1 = UserProfile.objects.create(case_manager=True)
 up2 = UserProfile.objects.create(case_manager=False)
 }}}

 With Microsoft SQL Server, the 2nd attempt to create a record with a NULL
 value results in an integrity error:

 django.db.utils.IntegrityError: ('23000', "[23000] [Microsoft][SQL Server
 Native Client 11.0][SQL Server]Violation of UNIQUE KEY constraint
 'UQ__contacts__3E2672343AF86B58'. Cannot insert duplicate key in object
 'dbo.contacts_userprofile'. The duplicate key value is (<NULL>). (2627)
 (SQLExecDirectW)")

 This post - http://stackoverflow.com/a/767702 describes the problem and
 suggests creating a "unique filtered index based on a predicate that
 excludes NULLs".

 I have confirmed that if I manually remove the constraint created by the
 migration in the table schema and create a unique filtered index that
 excludes NULLs on the company field I can create multiple records with a
 NULL value in the company field.  The migration should be adding a filter
 to the index that looks something like:

 {{{
 WHERE ([company_id] IS NOT NULL)
 }}}

 I am able to duplicate the problem with Django 1.9.10 and 1.10.4.

--
Ticket URL: <https://code.djangoproject.com/ticket/27627>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.bd1af5e1c2db4585d936bb37a8a27bea%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to