mirror of https://github.com/django/django.git
[5.1.x] Refs #35074 -- Avoided failed attempts to remove spatial indexes on nullable fields on MySQL.
MySQL doesn't support spatial indexes on NULL columns, so there is no
point in removing them.
Backport of 20c2d625d3
from main.
This commit is contained in:
parent
ba81b3f0a3
commit
adb72fa854
|
@ -54,7 +54,7 @@ class MySQLGISSchemaEditor(DatabaseSchemaEditor):
|
||||||
self.create_spatial_indexes()
|
self.create_spatial_indexes()
|
||||||
|
|
||||||
def remove_field(self, model, field):
|
def remove_field(self, model, field):
|
||||||
if isinstance(field, GeometryField) and field.spatial_index:
|
if isinstance(field, GeometryField) and field.spatial_index and not field.null:
|
||||||
index_name = self._create_spatial_index_name(model, field)
|
index_name = self._create_spatial_index_name(model, field)
|
||||||
sql = self._delete_index_sql(model, index_name)
|
sql = self._delete_index_sql(model, index_name)
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -134,6 +134,24 @@ class OperationTests(OperationTestCase):
|
||||||
if self.has_spatial_indexes:
|
if self.has_spatial_indexes:
|
||||||
self.assertSpatialIndexExists("gis_neighborhood", "path")
|
self.assertSpatialIndexExists("gis_neighborhood", "path")
|
||||||
|
|
||||||
|
@skipUnless(connection.vendor == "mysql", "MySQL specific test")
|
||||||
|
def test_remove_geom_field_nullable_with_index(self):
|
||||||
|
# MySQL doesn't support spatial indexes on NULL columns.
|
||||||
|
with self.assertNumQueries(1) as ctx:
|
||||||
|
self.alter_gis_model(
|
||||||
|
migrations.AddField,
|
||||||
|
"Neighborhood",
|
||||||
|
"path",
|
||||||
|
fields.LineStringField,
|
||||||
|
field_class_kwargs={"null": True},
|
||||||
|
)
|
||||||
|
self.assertColumnExists("gis_neighborhood", "path")
|
||||||
|
self.assertNotIn("CREATE SPATIAL INDEX", ctx.captured_queries[0]["sql"])
|
||||||
|
|
||||||
|
with self.assertNumQueries(1), self.assertNoLogs("django.contrib.gis", "ERROR"):
|
||||||
|
self.alter_gis_model(migrations.RemoveField, "Neighborhood", "path")
|
||||||
|
self.assertColumnNotExists("gis_neighborhood", "path")
|
||||||
|
|
||||||
@skipUnless(HAS_GEOMETRY_COLUMNS, "Backend doesn't support GeometryColumns.")
|
@skipUnless(HAS_GEOMETRY_COLUMNS, "Backend doesn't support GeometryColumns.")
|
||||||
def test_geom_col_name(self):
|
def test_geom_col_name(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|
Loading…
Reference in New Issue