Module 4 of 9 · Lesson 9 of 26
Load related data and paginate without repeats
Work through load related data and paginate without repeats using a runnable reference, a focused regression check and a local extension.
Read in any order. All lessons stay open, including after an unanswered or incorrect check.
Cursor pagination needs a total order. created_at alone is not unique: multiple rentals can share a timestamp. Order by (created_at, id), encode both values, and use a strict greater-than comparison on the next page. Apply the owner's scope before the cursor and limit, so forbidden records cannot influence a learner's page. The cursor is a position, not an authorization token; every request still applies current scope. This local example does not promise a snapshot of a dataset changing between requests. State that limitation rather than claiming all concurrent inserts are frozen. A returned next cursor may lead to an empty final page when a page is exactly full.
Run lesson09. Three rows share one timestamp, yet the first two and final one do not overlap. The same test reads each equipment name and requires exactly two queries. Decode a sample cursor in your own local debugger and identify both ordering values. Do not treat it as a secret access grant.
def page_statement(owner_id, cursor=None, limit=10, *, inclusive=False):
from sqlalchemy import tuple_
query = select(Rental).where(Rental.owner_id == owner_id)
if cursor:
boundary = cursor_values(cursor)
pair = tuple_(Rental.created_at, Rental.id)
query = query.where(pair >= boundary if inclusive else pair > boundary)
return query.order_by(Rental.created_at, Rental.id).limit(min(max(limit, 1), 100))
python run_checks.py -k lesson09
Expected result The selected lesson test passes against a new temporary PostgreSQL database; the container is removed afterward.
Keep for reference
Equipment Rental lab and lesson checks
ZIP containing Python source, real Alembic migrations, 26 lesson checks, a dependency lock and text instructions. Extract it before following the local exercise.
Practise locally
Create five same-timestamp rows owned by one user and two rows owned by another. Fetch pages of two until there is no next page; assert all five permitted UUIDs appear once and the other user's UUIDs never appear. Add a query-count assertion for the related equipment reads. Save the resulting id sequence and explain the tie-breaker.
The lesson check verifies the reference behavior. Add your own assertions for your change. Local practice is not uploaded or scored by this learning release.
Pause and reflect
What failure does this lesson prevent, and which assertion in lesson09 would expose it?
Use a concrete input, expected result and limitation from your local work. Saving a reflection does not certify the project.
Optional knowledge check
Which cursor boundary avoids repeating the last row of a page?
Compare strictly greater than timestamp, without an id tie-breaker.
Try another answer. Equal timestamps need a unique second ordering value to avoid skipped rows.
Strictly greater than the last (timestamp, id) pair.
Correct. A total order and strict boundary exclude the row already returned.
Greater than or equal to the last timestamp alone.
Try another answer. An inclusive boundary repeats rows, and a timestamp alone cannot distinguish ties.
This practice does not assess your project or award a certificate.
Your reading progress
Progress is saved in this browser when storage is available.