Opened 15 years ago

Closed 11 years ago

#11599 closed Bug (fixed)

Field exceptions are silently passed when slicing geoqueryset

Reported by: Jani Tiainen Owned by: nobody
Component: Database layer (models, ORM) Version: dev
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 by Malcolm Tredinnick, 15 years ago

Resolution: invalid
Status: newclosed

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 by Jani Tiainen, 15 years ago

Resolution: invalid
Status: closedreopened
>> 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 by Jani Tiainen, 15 years ago

It seems that this is GeoQuerySet related problem only.

comment:4 by mal, 15 years ago

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 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:6 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

comment:7 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 by Aymeric Augustin, 11 years ago

Status: reopenednew

comment:10 by Anssi Kääriäinen, 11 years ago

Resolution: fixed
Status: newclosed

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