From f77ea5506ca2aa2e125fcd6abfbb79cc016c5fbb Mon Sep 17 00:00:00 2001 From: Jon Michaelchuck Date: Fri, 18 Mar 2022 16:16:35 -0700 Subject: [PATCH 1/5] Use offset in the 'Combine Limit and Offset with Where' example --- docs_src/tutorial/offset_and_limit/tutorial004.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs_src/tutorial/offset_and_limit/tutorial004.py b/docs_src/tutorial/offset_and_limit/tutorial004.py index a95715cd98..f68a0a4f2a 100644 --- a/docs_src/tutorial/offset_and_limit/tutorial004.py +++ b/docs_src/tutorial/offset_and_limit/tutorial004.py @@ -43,7 +43,7 @@ def create_heroes(): def select_heroes(): with Session(engine) as session: - statement = select(Hero).where(Hero.age > 32).limit(3) + statement = select(Hero).where(Hero.age > 32).offset(6).limit(3) results = session.exec(statement) heroes = results.all() print(heroes) From 862b45e561f975d604b322e85a4721536825bb6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 23 Oct 2023 20:44:26 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20offset=20to?= =?UTF-8?q?=201,=20to=20make=20sense=20with=20the=20heroes=20inserted?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/tutorial/offset_and_limit/tutorial004.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs_src/tutorial/offset_and_limit/tutorial004.py b/docs_src/tutorial/offset_and_limit/tutorial004.py index f68a0a4f2a..a147d0ae8d 100644 --- a/docs_src/tutorial/offset_and_limit/tutorial004.py +++ b/docs_src/tutorial/offset_and_limit/tutorial004.py @@ -43,7 +43,7 @@ def create_heroes(): def select_heroes(): with Session(engine) as session: - statement = select(Hero).where(Hero.age > 32).offset(6).limit(3) + statement = select(Hero).where(Hero.age > 32).offset(1).limit(3) results = session.exec(statement) heroes = results.all() print(heroes) From 5fde46688d64ca3bba6fadf5b7993ace4a1635ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 23 Oct 2023 20:44:53 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9D=20Update=20docs=20with=20new?= =?UTF-8?q?=20offset=20query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/tutorial/limit-and-offset.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/docs/tutorial/limit-and-offset.md b/docs/tutorial/limit-and-offset.md index dc4c28063c..c8b0ddf72f 100644 --- a/docs/tutorial/limit-and-offset.md +++ b/docs/tutorial/limit-and-offset.md @@ -271,11 +271,11 @@ Of course, you can also combine `.limit()` and `.offset()` with `.where()` and o -## Run the Program with Limit and Where on the Command Line +## Run the Program with Limit, Offset, and Where on the Command Line If we run it on the command line, it will find all the heroes in the database with an age above 32. That would normally be 4 heroes. -But we are limiting the results to only get the first 3: +But we are starting to include after an offset of 1 (so we don't count the first one), and we are limiting the results to only get the first 2 after that:
@@ -284,18 +284,17 @@ $ python app.py // Previous output omitted 🙈 -// Select with WHERE and LIMIT +// Select with WHERE and LIMIT and OFFSET INFO Engine SELECT hero.id, hero.name, hero.secret_name, hero.age FROM hero WHERE hero.age > ? LIMIT ? OFFSET ? -INFO Engine [no key 0.00022s] (32, 3, 0) +INFO Engine [no key 0.00022s] (32, 2, 1) -// Print the heroes received, only 3 +// Print the heroes received, only 2 [ - Hero(age=35, secret_name='Trevor Challa', id=5, name='Black Lion'), - Hero(age=36, secret_name='Steve Weird', id=6, name='Dr. Weird'), - Hero(age=48, secret_name='Tommy Sharp', id=3, name='Rusty-Man') + Hero(age=36, id=6, name='Dr. Weird', secret_name='Steve Weird'), + Hero(age=48, id=3, name='Rusty-Man', secret_name='Tommy Sharp') ] ``` From 45a79e482edb4f5148572e29d5089947657a5a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 23 Oct 2023 20:45:18 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20offset=20to?= =?UTF-8?q?=201=20and=20limit=20to=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/tutorial/offset_and_limit/tutorial004.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs_src/tutorial/offset_and_limit/tutorial004.py b/docs_src/tutorial/offset_and_limit/tutorial004.py index a147d0ae8d..43828b853f 100644 --- a/docs_src/tutorial/offset_and_limit/tutorial004.py +++ b/docs_src/tutorial/offset_and_limit/tutorial004.py @@ -43,7 +43,7 @@ def create_heroes(): def select_heroes(): with Session(engine) as session: - statement = select(Hero).where(Hero.age > 32).offset(1).limit(3) + statement = select(Hero).where(Hero.age > 32).offset(1).limit(2) results = session.exec(statement) heroes = results.all() print(heroes) From a0546963ce1ef7881f1296ba46dfcea864cc185a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 23 Oct 2023 20:46:35 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=E2=9C=85=20Update=20tests=20for=20limit,?= =?UTF-8?q?=20where,=20offset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test_limit_and_offset/test_tutorial004.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/test_tutorial/test_limit_and_offset/test_tutorial004.py b/tests/test_tutorial/test_limit_and_offset/test_tutorial004.py index 448b191249..eb15a1560e 100644 --- a/tests/test_tutorial/test_limit_and_offset/test_tutorial004.py +++ b/tests/test_tutorial/test_limit_and_offset/test_tutorial004.py @@ -4,16 +4,6 @@ from ...conftest import get_testing_print_function -expected_calls = [ - [ - [ - {"id": 5, "name": "Black Lion", "secret_name": "Trevor Challa", "age": 35}, - {"id": 6, "name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36}, - {"id": 3, "name": "Rusty-Man", "secret_name": "Tommy Sharp", "age": 48}, - ] - ] -] - def test_tutorial(clear_sqlmodel): from docs_src.tutorial.offset_and_limit import tutorial004 as mod @@ -26,4 +16,11 @@ def test_tutorial(clear_sqlmodel): with patch("builtins.print", new=new_print): mod.main() - assert calls == expected_calls + assert calls == [ + [ + [ + {"name": "Dr. Weird", "secret_name": "Steve Weird", "age": 36, "id": 6}, + {"name": "Rusty-Man", "secret_name": "Tommy Sharp", "age": 48, "id": 3}, + ] + ] + ]