#33638 closed Bug (fixed)
Saving unique geography fields crashes in the admin on PostGIS.
Reported by: | Sebastian Clarke | Owned by: | Jacob Walls |
---|---|---|---|
Component: | GIS | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Claude Paroz | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (13)
comment:1 by , 3 years ago
Cc: | added |
---|---|
Summary: | Model Admin for GIS models cannot save model with point field when (geography=True, unique=True) with postgis backend → Saving unique geography fields crashes in the admin on PostGIS. |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 2 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 23 months ago
Patch needs improvement: | unset |
---|
comment:7 by , 23 months ago
Patch needs improvement: | set |
---|
comment:8 by , 23 months ago
Patch needs improvement: | unset |
---|
comment:9 by , 23 months ago
Patch needs improvement: | set |
---|
comment:10 by , 23 months ago
Patch needs improvement: | unset |
---|
comment:11 by , 23 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
Tentatively accepted for future investigation. I was able to reproduce this issue, however using geography type is really limited on PostGIS (see docs) and
__bboverlaps
is not an option in the general case. I wonder how we could improve this, I can imagine two options:unique
is used withgeography
and skip these fields in unique checks,geography
fields to ageometry
type in the query.What do you think?