Opened 7 years ago

Closed 3 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: 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 7 years ago by Malcolm Tredinnick

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

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

It seems that this is GeoQuerySet related problem only.

comment:4 Changed 7 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 7 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

comment:6 Changed 6 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:7 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:10 Changed 3 years ago by Anssi Kääriäinen

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