Sequelize limit behavior with one-to-many eager loading
21K reputation · 10 Jul 2023, 22:13 UTC
When implementing pagination in Sequelize, the limit and offset options are used to control the number of records returned from the database. However, a specific behavior occurs when these options are combined with the include property for one-to-many associations.
Because Sequelize generates SQL JOINs for eager loading, the resulting row count includes the joined child records. This often causes the ORM to return fewer parent records than the value specified in the limit option, as the limit is applied to the total number of joined rows rather than the unique parent entities.
While the separate: true option is documented as a way to handle this by executing queries in distinct steps, it changes the execution pattern of the data retrieval.
- Under what specific conditions does Sequelize automatically handle the join-induced limit truncation without requiring
separate: true? - How does the performance overhead of
separate: truecompare to using a subquery for pagination on large datasets?