[bug?] Geodjango migrations ignore change of dimension in MultiPointField

Hello,

I think I found a bug in GeoDjango migration mechanism.

Reproduction

  1. Create a simple model
class MultiPointTester(models.Model):
    multipoint = models.MultiPointField(
        dim=2,
        blank=True,
        null=True,
        default="SRID=4326;MULTIPOINT EMPTY",
    )

and test for it

class MultipointTest(TransactionTestCase):
    def test_multipoint_creation(self):
        instance = models.MultiPointTester()
        instance.save()
        self.assertEqual(instance.multipoint.ewkt, "SRID=4326;MULTIPOINT EMPTY")
  1. Generate migration for it ./manage.py makemigrations
  2. Run test - it succeeds :white_check_mark:
  3. Change dim from 2 to 3 and create migration
    It contains migration like this (it does not mention dim change):
  operations = [
      migrations.AlterField(
          model_name='multipointtester',
          name='multipoint',
          field=django.contrib.gis.db.models.fields.MultiPointField(
                blank=True, 
                default='SRID=4326;MULTIPOINT EMPTY', 
                null=True, srid=4326),
      ),
  ]
  1. Run test again - it fails :x: with error:
    E ValueError: Cannot alter field test.MultiPointTester.multipoint into test.MultiPointTester.multipoint - they do not properly define db_type (are you using a badly-written custom field?)
  2. Remove all existing migration files and again generate migration ./manage.py makemigrations
  3. Run test again - - it succeeds :white_check_mark:

Is is a bug, or do I make some kind of mistake in the code?

GIS migrations are still not in a good state:
https://code.djangoproject.com/query?component=GIS&status=assigned&status=new&summary=~migration&order=id&desc=1&col=id&col=summary&col=component&col=owner&col=type

We need contributors with interest in this topic and time…

1 Like

My issue seems to be a similar to the ones mentioned here.

I think it would be a good idea to report it in this bug tracker. It may add some useful context for other developers.
Maybe I will have some time and will be able to fix this issue and submit working PR.

Feel free to work on that. Migrating GIS fields is not as trivial as other fields, as you may have to manipulate data, depending on the parameter you change. But any contribution in this field is welcome!

I opened #36129 (Geodjango migrations ignore change of dimension in MultiPointField) – Django