The database object db created in our application contains all the functions and helpers from both SQLAlchemy and SQLAlchemy Object Relational Mapper (ORM). SQLAlchemy ORM associates user-defined Python classes with database tables, and instances of those classes (objects) with rows in their corresponding tables. The classes that mirror the database tables are referred to as models.

Create the Database

# create the database tables
with app.app_context():
    db.create_all()

This will create the database file within the project directory.

Defining Tables (Models)

Inserting Data

Accessing Data

Queries

readers = Reader.query.all() # get all rows from a table 

# prints the name for every reader in the table 
for reader in readers:
	print(reader.name) 
reader1 = Reader.query.get(1) # get reader from table where id = 1 
print(reader1.name) # print name of reader where id = 1

JOINING TABLES

# get all reviews from book where id = 13
book_13 = Book.query.get(13).reviews.all() 

# get the author associatioted with annotation where id = 331
author_331 = Annotations.query.get(331).author

Filters