# Can't iterate NoneType using offset and count on search method

**URL:** https://discuss.tryton.org/t/cant-iterate-nonetype-using-offset-and-count-on-search-method/4900
**Category:** Developer
**Created:** [January 7, 2022, 4:47pm UTC](https://discuss.tryton.org/t/cant-iterate-nonetype-using-offset-and-count-on-search-method/4900 "2022-01-07T16:47:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hodeinavarro](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/hodeinavarro/32/1185_2.png) [@hodeinavarro](https://discuss.tryton.org/u/hodeinavarro)
#### Post date: [January 7, 2022, 4:47pm UTC](https://discuss.tryton.org/t/cant-iterate-nonetype-using-offset-and-count-on-search-method/4900/1 "2022-01-07T16:47:11Z")

</div>

Hello,

I found this case where I’m building a web application and I write the search method params on the application and then parse them on the server and use all of them in a query like

```python
sales = Sale.search(domain, offset=offset, order=order, limit=limit, count=count)

```

I’m querying the route that uses this method from a page that requires pagination so I send a first query that returns the count of records of such query, then the same query without the count..

The problem comes when I navigate to the second page where my offset is already calculated and I queried it on the count query, resulting in this:

```sql
SELECT COUNT(*) FROM sale_sale WHERE state = 'quotation' AND company = '1' OFFSET 21;
 count 
-------
(0 rows)

```

```bash
Traceback (most recent call last):
  File "/home/hodei/dev/ingsys/client/.venv/lib/python3.10/site-packages/trytond/protocols/wrappers.py", line 237, in wrapper
    response = func(request, *args, **kwargs)
  File "/home/hodei/tryton_app/wrappers.py", line 37, in wrapper
    return func(*args, application, **kwargs)
  File "/home/hodei/tryton_app/routes.py", line 107, in sales
    sales = Sale.search(domain, offset=offset, order=order,
  File "/home/hodei/dev/ingsys/client/.venv/lib/python3.10/site-packages/trytond/model/modelsql.py", line 1322, in search
    return cursor.fetchone()[0]
TypeError: 'NoneType' object is not subscriptable

```

Without the offset this query returns the correct value

```sql
SELECT COUNT(*) FROM sale_sale WHERE state = 'quotation' AND company = '1';
 count 
-------
    33
(1 row)

```

This is an easy fix for me handling the query either on the client or the server but I wonder if this could be considered as a bug, a feature or just a bad use.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [January 7, 2022, 5:03pm UTC](https://discuss.tryton.org/t/cant-iterate-nonetype-using-offset-and-count-on-search-method/4900/2 "2022-01-07T17:03:53Z")

</div>

Indeed `offset` does not have sense for a `SELECT COUNT(*)`.  
I guess we could improve the behavior in [Add limit to search count (#10967) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue10967) by supporting offset using a subquery.

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [February 6, 2022, 5:04pm UTC](https://discuss.tryton.org/t/cant-iterate-nonetype-using-offset-and-count-on-search-method/4900/3 "2022-02-06T17:04:40Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
