CodexBloom - Programming Q&A Platform

Inconsistent Behavior with Slick's Query Result in Scala 2.13.8 When Using Paging

👀 Views: 111 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-27
slick scala postgresql Scala

I'm trying to figure out I've looked through the documentation and I'm still confused about I've been banging my head against this for hours... I'm experiencing an scenario with paging in Slick (version 3.3.3) where the query results aren't consistent across different pages. I have a simple query to fetch items from a database, and when I paginate the results using `.drop()` and `.take()`, I sometimes end up with duplicate entries across pages. Here's a snippet of what I'm trying: ```scala val pageSize = 10 val pageNumber = 1 val result = myTable.drop(pageNumber * pageSize).take(pageSize).result val action = result.map(_.toList) val dbAction = db.run(action) ``` I expected to see results for pages 0, 1, 2, etc., returning distinct entries, but I notice that when I fetch page 1, it sometimes returns entries that I already saw on page 0. I suspect this might be due to some sort of race condition or how Slick handles lazy evaluations. I've also tried switching to `result.map(_.distinct)` after the fetch, but that led to an `org.postgresql.util.PSQLException: behavior: DISTINCT is not supported` behavior in PostgreSQL. I tried adding a sort order to ensure consistency: ```scala val sortedResult = myTable.sortBy(_.id).drop(pageNumber * pageSize).take(pageSize).result ``` However, that didn't resolve the duplicates. Could this be caused by missing transactions, or is there something wrong in how I define my Slick queries? What's the best practice for reliably paginating through results with Slick while avoiding duplicates? Any insights would be appreciated. I'm working on a CLI tool that needs to handle this. Has anyone else encountered this? For context: I'm using Scala on Windows. Has anyone else encountered this?