]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
📝 Update docs, use `offset` in example with `limit` and `where` (#273)
authorJon Michaelchuck <5964742+jbmchuck@users.noreply.github.com>
Mon, 23 Oct 2023 16:55:53 +0000 (09:55 -0700)
committerGitHub <noreply@github.com>
Mon, 23 Oct 2023 16:55:53 +0000 (20:55 +0400)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
docs/tutorial/limit-and-offset.md
docs_src/tutorial/offset_and_limit/tutorial004.py
tests/test_tutorial/test_limit_and_offset/test_tutorial004.py

index dc4c28063cd49243974ca59517ea9bb7fd85876a..c8b0ddf72fe2c7087844b775f86696ff0366bf93 100644 (file)
@@ -271,11 +271,11 @@ Of course, you can also combine `.limit()` and `.offset()` with `.where()` and o
 
 </details>
 
-## 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:
 
 <div class="termy">
 
@@ -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')
 ]
 ```
 
index a95715cd98d16a555ead909b86a976fa699dbd86..43828b853f0b0889308a4301fca66316938c24ee 100644 (file)
@@ -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(1).limit(2)
         results = session.exec(statement)
         heroes = results.all()
         print(heroes)
index 448b191249ab19f21f922222c954bd20bb21b01a..eb15a1560e2a459abbdaf97ec2e625b11e7979ab 100644 (file)
@@ -4,16 +4,6 @@ from sqlmodel import create_engine
 
 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},
+            ]
+        ]
+    ]