From 17205a98ed0e4fb2a4ce9137508ee65efc448c07 Mon Sep 17 00:00:00 2001
From: Tim Graham <timograham@gmail.com>
Date: Fri, 31 Oct 2014 09:04:46 -0400
Subject: [PATCH] [1.7.x] Fixed #23731 -- Fixed migrations crash when adding
 blank GeometryFields on PostGIS.

Thanks raratiru for the report and Claude Paroz for review.

Backport of d6d55368d4 from master
---
 django/contrib/gis/db/models/fields.py        |  2 +-
 .../tests/gis_migrations/test_operations.py   | 29 +++++++++++++++++++
 docs/releases/1.7.2.txt                       |  3 ++
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/django/contrib/gis/db/models/fields.py b/django/contrib/gis/db/models/fields.py
index b71bba7725..6d637fd8a7 100644
--- a/django/contrib/gis/db/models/fields.py
+++ b/django/contrib/gis/db/models/fields.py
@@ -274,7 +274,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 92abf99b90..69e72c417f 100644
--- a/django/contrib/gis/tests/gis_migrations/test_operations.py
+++ b/django/contrib/gis/tests/gis_migrations/test_operations.py
@@ -69,6 +69,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",
@@ -90,11 +91,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`).