﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33638	Saving unique geography fields crashes in the admin on PostGIS.	Sebastian Clarke	Jacob Walls	"If you define a model with a geometry point field that has geography=True and unique=True, the default admin interface generated for this model cannot save an existing instance.

When you save, you ultimately hit the error:

{{{
ValueError: PostGIS geography does not support the ""~="" function/operator.
}}}

This is caused by the `validate_unique()` method on the model instance. When performing unique checks (in `_perform_unique_checks()`), it's forming a queryset with `objects.filter(geometry=geometry)` and (as noted in related issue https://code.djangoproject.com/ticket/27314) that results in the ~= operator being used in PostGIS which is not supported on geographic geometries.

We will work around this issue ourselves, by overriding `validate_unique()` on the model, passing the field in question to the exclude list in the super call, then doing our own manual check by instead using a filter like:

{{{
GeoModel.objects.filter(geometry__bboverlaps=point)
}}}

i.e., a bounding-box overlaps check.

For point fields, the concept of even having a bounding box, and how much sense that makes, I suppose is up for debate (points theoretically being of infinitely small size). However this check performs in the way we want and works as you might expect the equality operator to.

Have not confirmed whether the issue exists for geometry fields other than Point, or on versions other than 3.2 - but I strongly suspect so.

Simplest test case would be to have a models.py:

{{{
from django.contrib.gis.db import models

class Location(models.Model):
    geometry = models.PointField(geography=True, unique=True)
}}}

along with admin.py

{{{
from django.contrib.gis import admin
admin.site.register(Location, admin.OSMGeoAdmin)
}}}

and then try and save an existing instance when using the postgis db backend."	Bug	closed	GIS	3.2	Normal	fixed		Claude Paroz	Ready for checkin	1	0	0	0	0	0
