Skip to content

Commit 1dbc273

Browse files
committed
Document IN()
1 parent 43a689d commit 1dbc273

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

docs/tutorial/where.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ age=32 id=4 name='Tarantula' secret_name='Natalia Roman-on'
665665

666666
### Less Than or Equal
667667

668-
Finally, we can use `<=` to get the rows where a column is **less than or equal** to a value:
668+
We can also use `<=` to get the rows where a column is **less than or equal** to a value:
669669

670670
```Python hl_lines="5"
671671
# Code above omitted 👆
@@ -694,6 +694,34 @@ age=35 id=5 name='Black Lion' secret_name='Trevor Challa'
694694
!!! tip
695695
We get `Black Lion` here too because although the age is not *strictly* less than `35` it is *equal* to `35`.
696696

697+
### In
698+
699+
Finally, we can use `in_ to get the rows where a column is a member of a collection of values:
700+
701+
```Python hl_lines="5"
702+
# Code above omitted 👆
703+
704+
{!./docs_src/tutorial/where/tutorial0066.py[ln:44-49]!}
705+
706+
# Code below omitted 👇
707+
```
708+
709+
<details>
710+
<summary>👀 Full file preview</summary>
711+
712+
```Python
713+
{!./docs_src/tutorial/where/tutorial0065.py!}
714+
```
715+
716+
</details>
717+
718+
In this case, we match `Deadpond` since it's part of the collections of names.
719+
We don't have any hero called `Ratman`, so it does not match any hero.
720+
721+
!!! tip
722+
You need to wrap your attribute with `col()` to use `in_`.
723+
You can read more about it in the (Type annotations and errors)[#type-annotations-and-errors] section.
724+
697725
### Benefits of Expressions
698726

699727
Here's a good moment to see that being able to use these pure Python expressions instead of keyword arguments can help a lot. ✨
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, Session, SQLModel, col, create_engine, select
4+
5+
6+
class Hero(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str
9+
secret_name: str
10+
age: Optional[int] = None
11+
12+
13+
sqlite_file_name = "database.db"
14+
sqlite_url = f"sqlite:///{sqlite_file_name}"
15+
16+
engine = create_engine(sqlite_url, echo=True)
17+
18+
19+
def create_db_and_tables():
20+
SQLModel.metadata.create_all(engine)
21+
22+
23+
def create_heroes():
24+
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
25+
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
26+
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)
27+
hero_4 = Hero(name="Tarantula", secret_name="Natalia Roman-on", age=32)
28+
hero_5 = Hero(name="Black Lion", secret_name="Trevor Challa", age=35)
29+
hero_6 = Hero(name="Dr. Weird", secret_name="Steve Weird", age=36)
30+
hero_7 = Hero(name="Captain North America", secret_name="Esteban Rogelios", age=93)
31+
32+
with Session(engine) as session:
33+
session.add(hero_1)
34+
session.add(hero_2)
35+
session.add(hero_3)
36+
session.add(hero_4)
37+
session.add(hero_5)
38+
session.add(hero_6)
39+
session.add(hero_7)
40+
41+
session.commit()
42+
43+
44+
def select_heroes():
45+
with Session(engine) as session:
46+
statement = select(Hero).where(col(Hero.name).in_(["Deadpond", "Ratman"]))
47+
results = session.exec(statement)
48+
for hero in results:
49+
print(hero)
50+
51+
52+
def main():
53+
create_db_and_tables()
54+
create_heroes()
55+
select_heroes()
56+
57+
58+
if __name__ == "__main__":
59+
main()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from unittest.mock import patch
2+
3+
from sqlmodel import create_engine
4+
5+
from ...conftest import get_testing_print_function
6+
7+
8+
def test_tutorial(clear_sqlmodel):
9+
from docs_src.tutorial.where import tutorial0065 as mod
10+
11+
mod.sqlite_url = "sqlite://"
12+
mod.engine = create_engine(mod.sqlite_url)
13+
calls = []
14+
15+
new_print = get_testing_print_function(calls)
16+
17+
with patch("builtins.print", new=new_print):
18+
mod.main()
19+
assert calls == [
20+
[
21+
{
22+
"name": "Deadpond",
23+
"secret_name": "Dive Wilson",
24+
"age": None,
25+
"id": 1,
26+
}
27+
]
28+
]

0 commit comments

Comments
 (0)