Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds
db.paginate,db.get_or_404,db.first_or_404, anddb.one_or_404methods. These operate onselectstatements instead of the legacyqueryobject. fixes #1088The
Paginationobject should no longer be created manually. The constructor signature changed to actually apply the pagination, but it is also a base class that will raise aNotImplementedError.db.paginatecreates aSelectPaginationobject, andquery.paginatecreates aQueryPaginationobject. This refactor made it very easy to create the two subclasses without duplicating code, but I decided to keep the public API in thepaginatemethods. Moving away from providing a generic pagination API was previously discussed in #282.When querying a model,
Query.allwould automatically return single objects instead of tuples and dedupe joins. There doesn't seem to be a way to get the same automatic checks withselect. We need to calluniqueandscalars, which meansdb.paginatecurrently only works forselect(Model), not forselect(Model.column, Model.column).I'm going to open a discussion on the SQLAlchemy repo, but I'm not really sure if we're missing something with the new
execute(select())pattern. It's more verbose thanModel.query, and doesn't provide some of the old query behavior.Model.selectwouldn't really be useful, sincedbis already needed fordb.session.execute, andModel.selectisn't much shorter thandb.select(User). We could add adb.executemethod as a shortcut fordb.session.execute, but I'm not sure that's worth it either.