Opened 6 years ago

Closed 2 years ago

#11599 closed Bug (fixed)

Field exceptions are silently passed when slicing geoqueryset

Reported by: jtiai Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using GeoDjango exception happens when populating model from database using get().

x = MyModel.objects.get(pk=1)

Above works as expected, correct exception is thrown

But:

qs = MyModel.objects.filter(pk=1)
qs[0] 

Now IndexError exception is thrown instead of exception that was expected (in my particular case, there was invalid geometry).

Returned list from queryset itself is empty, no error thrown at all.

Change History (10)

comment:1 Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

This isn't incorrect behaviour. The second example returns a Queryset. Always. It happens that the queryset is empty in your case. When you index into the queryset using a single index like this, it is treated the same as indexing into a list. You are asking for a non-existent element, so an IndexError is raised.

comment:2 Changed 6 years ago by jtiai

  • Resolution invalid deleted
  • Status changed from closed to reopened
>> x = MyModel.objects.get(pk=1)
>> TypeError: cannot set MyModel GeometryProxy with value of type: <class 'django.contrib.gis.geos.collections.MultiLineString'>

But

qs = MyModel.objects.filter(pk=1)

using qs.Count() with returns 1, but qs[0] returns IndexError. len(qs) raises TypeError again.

So Count(), len and slicing are not consistent.

comment:3 Changed 6 years ago by jtiai

It seems that this is GeoQuerySet related problem only.

comment:4 Changed 6 years ago by mal

Yes, slicing geoqueryset fails with IndexError when query result contains geometry field of incorrect type (e.g when model is defined with LineString but database contains MultiLineString).

comment:5 Changed 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:9 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

comment:10 Changed 2 years ago by akaariai

  • Resolution set to fixed
  • Status changed from new to closed

I believe this is part of the more general "exceptions in queryset iteration are swallowed, empty list is returned" problem that has been resolved in master.

Note: See TracTickets for help on using tickets.
Back to Top