# Off by one error in latest.json page 1

**URL:** https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622
**Category:** Bug
**Created:** [Março 8, 2017, 9:46am UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622 "2017-03-08T09:46:44Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [Março 8, 2017, 9:46am UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/1 "2017-03-08T09:46:44Z")

</div>

latest.json page 0 , `topic_list.topics[29]` is the same item as page 1 `topic_list.topics[0]`

 ![](https://global.discourse-cdn.com/meta/original/3X/2/e/2e3a0a3e607484ec700ff36074221c1147daac21.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/4/0/407afe4e104389142b77f0f2a9910d435016f31a.png)

This only seems to happen between pages 0 and 1.

I can’t find the code that is causing this.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [Março 8, 2017, 2:17pm UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/2 "2017-03-08T14:17:34Z")

</div>

@blake can you add to your list, but we need to make sure @eviltrout or myself review this carefully prior to merging cause I worry this could break the front end if it is somehow depending on this super odd behavior.

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [Março 16, 2017, 3:23pm UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/4 "2017-03-16T15:23:20Z")

</div>

I also looked into this and it seems that this bug is related only to pages 0 and 1.

The selection queries are something like:

```
SELECT ... LIMIT 30; -- Query A (page 0)
SELECT ... LIMIT 30 OFFSET 29; -- Query B (page 1)
SELECT ... LIMIT 30 OFFSET 59; -- Query C (page 2)

```

Query A is going to select first 30 rows (1-30); query B is going to select 30 rows, but would skip the first 29 (30-59 – this is why row 30 is duplicated); and query C is going to select 30 rows, but would skip the first 59, so (60-89).

The fix would be to always skip `page * 30`. Currently the formula is `page * 30 - 1`.

Unfortunately, I do not know where exactly to look to fix this issue. I have been looking through the ListController, but failed. Any suggestions are appreciated.

Also, I believe that this will not affect the front-end, but I cannot say for sure.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [Março 16, 2017, 3:34pm UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/5 "2017-03-16T15:34:25Z")

</div>

We need to be careful here, cause it is not impossible that the client is expecting overlap. Be sure to ensure scrolling latest continues to work if you apply this fix.

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [Março 16, 2017, 5:06pm UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/6 "2017-03-16T17:06:07Z")

</div>

> [@nbianca](#):
>
> Unfortunately, I do not know where exactly to look to fix this issue. I have been looking through the ListController, but failed. Any suggestions are appreciated.

The code is in `lib/topic_query.rb`

This looks ok:

> <https://github.com/discourse/discourse/blob/main/lib/topic_query.rb#L570-L573>

but then this does not:

> <https://github.com/discourse/discourse/blob/main/lib/topic_query.rb#L305-L311>

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [Março 20, 2017, 4:28pm UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/7 "2017-03-20T16:28:35Z")

</div>

I looked into this and it really is the second snippet of code the one that causes the issue. I made the changes (removed the `- 1`), did some testing and everything works as expected. I will be testing more.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [Maio 9, 2017, 5:58pm UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/8 "2017-05-09T17:58:20Z")

</div>

I recall seeing a PR to fix this, did it ever get merged in?

---

<div class="post-metadata">

### Author: ![nbianca](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nbianca/32/157984_2.png) [@nbianca](https://meta.discourse.org/u/nbianca)
#### Post date: [Maio 9, 2017, 7:24pm UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/9 "2017-05-09T19:24:46Z")

</div>

I too remember sending a PR, but it looks like I never did. I did some work on this issue and tested it, but probably I considered that more testing was required and that’s why I did not finalize sending the PR.

Anyway, here it is:  
[https://github.com/discourse/discourse/pull/4858](https://github.com/discourse/discourse/pull/4858)

Since it is an important part of the system, I would really appreciate if you could look into it as well.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [Dezembro 21, 2017, 12:15am UTC](https://meta.discourse.org/t/off-by-one-error-in-latest-json-page-1/58622/10 "2017-12-21T00:15:52Z")

</div>


