Skip to content

Commit 89c874f

Browse files
authored
django: allow changing nullability of fields with db_index=True (#415)
Changing nullability of fields with indexes from Meta.indexes isn't supported. refs #378
1 parent 1097281 commit 89c874f

File tree

1 file changed

+30
-0
lines changed
  • packages/django-google-spanner/django_spanner

1 file changed

+30
-0
lines changed

packages/django-google-spanner/django_spanner/schema.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# license that can be found in the LICENSE file or at
55
# https://developers.google.com/open-source/licenses/bsd
66

7+
from django.db import NotSupportedError
78
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
89

910

@@ -202,6 +203,35 @@ def quote_value(self, value):
202203
# https://github.com/orijtech/django-spanner/issues/227
203204
return str(value)
204205

206+
def _alter_field(self, model, old_field, new_field, old_type, new_type,
207+
old_db_params, new_db_params, strict=False):
208+
# Spanner requires dropping indexes before changing the nullability
209+
# of a column.
210+
nullability_changed = old_field.null != new_field.null
211+
if nullability_changed:
212+
index_names = self._constraint_names(
213+
model, [old_field.column], index=True,
214+
)
215+
if index_names and not old_field.db_index:
216+
raise NotSupportedError(
217+
"Changing nullability of a field with an index other than "
218+
"Field(db_index=True) isn't yet supported."
219+
)
220+
if len(index_names) > 1:
221+
raise NotSupportedError(
222+
"Changing nullability of a field with more than one "
223+
"index isn't yet supported."
224+
)
225+
for index_name in index_names:
226+
self.execute(self._delete_index_sql(model, index_name))
227+
super()._alter_field(
228+
model, old_field, new_field, old_type, new_type,
229+
old_db_params, new_db_params, strict=False,
230+
)
231+
# Recreate the index that was dropped earlier.
232+
if nullability_changed and new_field.db_index:
233+
self.execute(self._create_index_sql(model, [new_field]))
234+
205235
def _alter_column_type_sql(self, model, old_field, new_field, new_type):
206236
# Spanner needs to use sql_alter_column_not_null if the field is
207237
# NOT NULL, otherwise the constraint is dropped.

0 commit comments

Comments
 (0)