Skip to content

Commit

Permalink
Allow for unique NULL based on https://github.com/michiya/django-pyod…
Browse files Browse the repository at this point in the history
  • Loading branch information
wgordon17 committed May 13, 2019
1 parent c827b96 commit 9c99990
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 0 additions & 1 deletion sql_server/pyodbc/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_index_on_text_field = False
supports_nullable_unique_constraints = False
supports_paramstyle_pyformat = False
supports_partially_nullable_unique_constraints = False
supports_regex_backreferencing = False
supports_sequence_reset = False
supports_subqueries_in_group_by = False
Expand Down
22 changes: 21 additions & 1 deletion sql_server/pyodbc/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
sql_delete_table = "DROP TABLE %(table)s"
sql_rename_column = "EXEC sp_rename '%(table)s.%(old_column)s', %(new_column)s, 'COLUMN'"
sql_rename_table = "EXEC sp_rename %(old_table)s, %(new_table)s"
sql_create_unique_null = "CREATE UNIQUE INDEX %(name)s ON %(table)s(%(columns)s) " \
"WHERE %(columns)s IS NOT NULL"

def _alter_column_default_sql(self, model, old_field, new_field, drop=False):
"""
Expand Down Expand Up @@ -320,7 +322,15 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
self._delete_primary_key(model, strict)
# Added a unique?
if self._unique_should_be_added(old_field, new_field):
self.execute(self._create_unique_sql(model, [new_field.column]))
if new_field.null:
self.execute(
self._create_index_sql(
model, [new_field], sql=self.sql_create_unique_null, suffix="_uniq"
)
)
else:
self.execute(self._create_unique_sql(model, [new_field.column]))

# Added an index?
# constraint will no longer be used in lieu of an index. The following
# lines from the truth table show all True cases; the rest are False:
Expand Down Expand Up @@ -496,6 +506,11 @@ def add_field(self, model, field):
# It might not actually have a column behind it
if definition is None:
return
if field.null and field.unique:
definition = definition.replace(' UNIQUE', '')
self.deferred_sql.append(self._create_index_sql(
model, [field], sql=self.sql_create_unique_null, suffix="_uniq"
))
# Check constraints can go on the column SQL here
db_params = field.db_parameters(connection=self.connection)
if db_params['check']:
Expand Down Expand Up @@ -538,6 +553,11 @@ def create_model(self, model):
definition, extra_params = self.column_sql(model, field)
if definition is None:
continue
if field.null and field.unique:
definition = definition.replace(' UNIQUE', '')
self.deferred_sql.append(self._create_index_sql(
model, [field], sql=self.sql_create_unique_null, suffix="_uniq"
))
# Check constraints can go on the column SQL here
db_params = field.db_parameters(connection=self.connection)
if db_params['check']:
Expand Down

0 comments on commit 9c99990

Please sign in to comment.