Sequelize Pagination: Limit and Distinct behavior during Eager Loading
0 reputation · 27 Mar 2024, 07:36 UTC
When implementing pagination in Sequelize (v6+), the limit and offset options are used to control the result set size. However, a specific complexity arises when utilizing include for one-to-many associations alongside these pagination constraints.
To prevent the joined result set from inflating the row count, Sequelize often generates a subquery. When multiple associations are involved, the interaction between the limit option and the distinct: true configuration becomes critical to ensure the main model is paginated correctly rather than the total number of joined rows.
There is uncertainty regarding how the distinct flag affects the generated SQL when joining across multiple levels of associations, specifically concerning whether the limit is applied to the unique primary keys of the main model or the resulting join table.
- Does
distinct: trueconsistently ensure thelimitapplies to the main model's primary keys across all supported SQL dialects? - What is the expected behavior of the row count when
distinctis omitted during a paginated eager load of multiple one-to-many associations?