ORM - How to do waterfall order by query for uuid primary key pagination

I am trying to do pagination with created at and uuid primary key. After doing some research came across, indexing - How to keypaginate UUID in postgresql? - Stack Overflow. Been trying to create a ORM query for the same, can someone help with this. Otherwise will have to write the raw query.

Thanks in advance.

Can you be more specific with what you’re trying to achieve here? What is your final objective? Maybe provide more detail about what problem or issue you’re trying to address. (What do you mean by ‘waterfall order’?)

Hi @KenWhitesell,

I want to do order things on the basis of two keys (created_at, uuid) and along with that add pagination as well.

Basically running this query,

WHERE (created_at, uuid) > (last_created_at, last_uuid)
ORDER BY created_at, uuid
LIMIT <page size>;

With waterfall ordering, I mean that it should paginate using created_at, but if two created_at matches then it should order using uuid but not otherwise.

From the way you’re describing this, you’re mixing up three separate and distinct issues.

  • For pagination, see Pagination.

  • For the ORDER BY, you can specify multiple fields in the order_by clause.

  • The WHERE clause constrains what rows are selected - it has nothing to do with paging. I’m not clear on what you’re trying to do with that clause here. Under what conditions do you wish to include (or exclude) rows?