﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27627	OneToOneField doesn't allow multiple records with NULL value on Microsoft SQL Server	Ryan Schave	nobody	"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.
"	Bug	closed	Database layer (models, ORM)		Normal	invalid		Michael Manfre	Accepted	0	0	0	0	0	0
