﻿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
16436	`annotate()` + `select_related()` + `only()` = `ValueError: invalid literal for int() with base 10`	Tai Lee	nobody	"With some combinations of models and arguments to `annotate()`, `select_related()` and `only()`, I get `ValueError: invalid literal for int() with base 10`.

I think that [https://code.djangoproject.com/browser/django/trunk/django/db/models/sql/compiler.py#L720 SQLCompiler.results_iter()] is not taking the deferred fields into account when calculating the `aggregate_start` offset.

To create a test, I started with a large number of models each with a large number of fields, taken from an app that we have in production, and pared it down by trial and error to as simple a form as I could and still reproduce the error.

As I did this (choosing models and fields to remove at random) the behaviour changed from raising a `ValueError`, to yielding no items when consuming the queryset (but with `.count()` still returning a positive number), to apparently working as expected.

This is probably related to (or the same issue) that was reported in the comments of #11890.

I had a crack at calculating the correct `aggregate_start` by looking at the `deferred_loading` attribute and the `q.get_loaded_field_names()` method on the `Query` object, but didn't have any luck.

I've put the test in it's own module, `defer_annotate_select_related_only`, just because it seems like such an obscure edge-case and I didn't want the other models and tests in `defer_regress` to have any influence.

Hopefully if someone can figure out how and why this is happening, we can integrate a more concise test into `defer_regress`.
"	Bug	closed	Database layer (models, ORM)	dev	Release blocker	fixed	annotate select_related only defer ValueError empty list	slafs@…	Accepted	1	0	0	1	0	0
