斯卡拉浮油.错误的分页\ w Postgres [英] Scala Slick. Wrong pagination \w Postgres

查看:112
本文介绍了斯卡拉浮油.错误的分页\ w Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Slick和Postgres进行简单的分页,但它没有 按预期工作.

I'm trying to make simple pagination using Slick with Postgres, but it does not work as expected.

// Table 
// "users_pkey" PRIMARY KEY, btree (id)
// "users_id_idx" btree (id)
val id: Column[Option[Long]] = column("id", O.PrimaryKey, O.AutoInc, O.DBType(BigSerial))

// Pagination queries
users.drop(0).take(20).sortBy(_.id.desc).list
users.drop(20).take(20).sortBy(_.id.desc).list

但是结果未按预期排序.内页上按id排序的用户,例如第一页将类似于40, 35, 34 ... 4, 2,第二页将类似于39, 38, 36, ... 3, 1.

But results are not ordered as expected. Users ordered by id on inside page, e.g. first page will be like 40, 35, 34 ... 4, 2 and second 39, 38, 36, ... 3, 1.

推荐答案

问题是您要在之后进行排序.如果39按照最初的顺序位于第21位,并且您对前20个值进行排序并对其进行排序,则无法将其排序到正确的位置,因为您没有在该位置对其进行排序.

The issue is that you are sorting after taking your values. If, in the initial order, 39 is in the 21st position and you take and sort the first 20 values, it cannot be sorted into the correct place, as you sort without it there.

如果您想正确排序,则应该在 之前对用户进行排序,例如:

If you want correct ordering, you should sort your users before taking the chunks, e.g:

val sortedUsers = users.sortBy(_.id.desc)
sortedUsers.drop(0).take(20).list
sortedUsers.drop(20).take(20).list

这篇关于斯卡拉浮油.错误的分页\ w Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆