diff --git a/django/contrib/gis/db/models/fields.py b/django/contrib/gis/db/models/fields.py index e260f67cbf..c538c18d63 100644 --- a/django/contrib/gis/db/models/fields.py +++ b/django/contrib/gis/db/models/fields.py @@ -277,7 +277,7 @@ class GeometryField(Field): def get_db_prep_save(self, value, connection): "Prepares the value for saving in the database." - if value is None: + if not value: return None else: return connection.ops.Adapter(self.get_prep_value(value)) diff --git a/django/contrib/gis/tests/gis_migrations/test_operations.py b/django/contrib/gis/tests/gis_migrations/test_operations.py index c979317630..8052e7ab1d 100644 --- a/django/contrib/gis/tests/gis_migrations/test_operations.py +++ b/django/contrib/gis/tests/gis_migrations/test_operations.py @@ -67,6 +67,7 @@ class OperationTests(TransactionTestCase): Tests the AddField operation with a GIS-enabled column. """ project_state = self.set_up_test_model() + self.current_state = project_state operation = migrations.AddField( "Neighborhood", "path", @@ -88,11 +89,39 @@ class OperationTests(TransactionTestCase): indexes = connection.introspection.get_indexes(cursor, "gis_neighborhood") self.assertIn('path', indexes) + def test_add_blank_gis_field(self): + """ + Should be able to add a GeometryField with blank=True. + """ + project_state = self.set_up_test_model() + self.current_state = project_state + operation = migrations.AddField( + "Neighborhood", + "path", + fields.LineStringField(blank=True, srid=4326), + ) + new_state = project_state.clone() + operation.state_forwards("gis", new_state) + with connection.schema_editor() as editor: + operation.database_forwards("gis", editor, project_state, new_state) + self.current_state = new_state + self.assertColumnExists("gis_neighborhood", "path") + + # Test GeometryColumns when available + if HAS_GEOMETRY_COLUMNS: + self.assertGeometryColumnsCount(2) + + if self.has_spatial_indexes: + with connection.cursor() as cursor: + indexes = connection.introspection.get_indexes(cursor, "gis_neighborhood") + self.assertIn('path', indexes) + def test_remove_gis_field(self): """ Tests the RemoveField operation with a GIS-enabled column. """ project_state = self.set_up_test_model() + self.current_state = project_state operation = migrations.RemoveField("Neighborhood", "geom") new_state = project_state.clone() operation.state_forwards("gis", new_state) diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index d5fe622278..7b455db62e 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -40,3 +40,6 @@ Bugfixes * Fixed a migration crash that prevented changing a nullable field with a default to non-nullable with the same default (:ticket:`23738`). + +* Fixed a migrations crash when adding ``GeometryField``\s with ``blank=True`` + on PostGIS (:ticket:`23731`).