-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathdocs002.py
More file actions
51 lines (38 loc) · 1.23 KB
/
Copy pathdocs002.py
File metadata and controls
51 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import asyncio
import sqlalchemy
from examples import create_drop_database
import ormar
from ormar import DatabaseConnection
DATABASE_URL = "sqlite+aiosqlite:///queries_docs002.db"
database = DatabaseConnection(DATABASE_URL)
metadata = sqlalchemy.MetaData()
ormar_base_config = ormar.OrmarConfig(
database=database,
metadata=metadata,
)
class Book(ormar.Model):
ormar_config = ormar_base_config.copy(
tablename="books",
)
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(
max_length=100,
default="Fiction",
)
@create_drop_database(base_config=ormar_base_config)
async def run_query():
await Book.objects.create(
title="Tom Sawyer", author="Twain, Mark", genre="Adventure"
)
await Book.objects.create(
title="War and Peace", author="Tolstoy, Leo", genre="Fiction"
)
await Book.objects.create(
title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction"
)
await Book.objects.update(each=True, genre="Fiction")
all_books = await Book.objects.filter(genre="Fiction").all()
assert len(all_books) == 3
asyncio.run(run_query())