-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_db.py
31 lines (23 loc) · 1.22 KB
/
test_db.py
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
from sqlalchemy import create_engine, select
from sqlalchemy.orm import sessionmaker
from models import User, Project, Task
# Set up the database connection (NO ES IGUAL A FLASK. NO ESTAMOS EN UN PROCESO DE FLASK AHORA!!!!)
engine = create_engine('sqlite:///./instance/tasks.db')
Session = sessionmaker(bind=engine)
session = Session()
#--------------------------------------------------------------------------
print(f"SQLAlchemy example using SQL style select and not the deprecated Query interface")
# Retrieve the user with the name "john" and ID 1
john = session.execute(select(User).where((User.name == 'john') & (User.id == 1))).scalar_one()
# Retrieve all tasks for a project with ID 1 and associated with John
tasks = session.execute(select(Task).join(Task.project).join(Project.users).where((Project.id == 1) & (User.id == john.id))).scalars()
# Print the task names
for task in tasks:
print(f"task: {task.name}")
#--------------------------------------------------------------------------
print(f"SQLAlchemy example more in tune with Flask ORM atterns")
john = session.execute(select(User).filter_by(id=1)).scalar_one()
tasks = john.projects[0].tasks
# Print the task names
for task in tasks:
print(f"task: {task.name}")