Flask Part3
Flask Part3
z
DATABASE
2
z
SQL Databases
Relational databases store data in tables.
The columns define the data attributes of the entity represented by the
table.
Each row in a table defines an actual data element that assigns values to
some or all the columns.
Tables have a special column called the primary key, which holds a unique
identifier for each row stored in the table.
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
Tables can also have columns called foreign keys, which reference the
primary key of a row in the same or another table.
5/27/2023
3
z
NoSQL Databases
Databases that do not follow the relational are collectively
referred to as NoSQL databases.
Example:
5/27/2023
4
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
5/27/2023
5
z
Python Database Frameworks
Python has packages for most database engines, both open source and
commercial.
Ease of use
Performance
Portability
Flask integration
5/27/2023
6
z
Database Management with Flask-
SQLAlchemy
Flask-SQLAlchemy is a Flask extension that
simplifies the use of SQLAlchemy inside Flask
applications.
as a URL.
7
z
Model De€
nition
z
Relationships
z
Database Operations
the most common data‐ base operations in a shell started with
the flask shell command.
z
Inserting Rows
The constructors for models accept initial values for the model
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
z
Modifying Rows
Example:
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
5/27/2023
18
z
Deleting Rows
z
Querying Rows
Flask-SQLAlchemy makes a query object available in each
model class.
The most basic query for a model is triggered with the all()
method, which returns the entire contents of the corresponding
table:
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
z
Database Use in View Functions
The database
operations described
in the previous
sections can be used
directly inside view
functions.
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
Example:
5/27/2023
23
z
Database Use in View Functions
To avoid having to constantly repeat these steps, the flask shell command
can be configured to automatically import these objects.
To add objects to the import list, a shell context processor must be created
and regis‐ tered with the app.shell_context_processor decorator.
The flask shell command will import these items auto‐ matically into the shell,
in addition to app, which is imported by default:
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
5/27/2023
25
z
Database Migrations with Flask-Migrate
command.
5/27/2023
26
Creating a Migration Repository
z
To begin, Flask-Migrate must be installed in the virtual environment:
z
Creating a Migration Script
In Alembic, a database migration is represented by a migration script.
The upgrade() function applies the database changes that are part of the migration.
Alembic migrations can be created manually or automatically using the revision and migrate
commands, respectively.
A manual migration creates a migration skeleton script with empty upgrade() and
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE
An automatic migration attempts to generate the code for the upgrade() and downgrade()
functions by looking for differences between the model definitions and the current state of
the database.
5/27/2023
28
5. Apply the migration to the database with the flask db upgrade command.
z
Upgrading the Database
z
Adding More Migrations
inaccuracies.
z
If your last migration has not been committed to source control
yet, you can opt to expand it to incorporate new changes.
1. Remove the last migration from the database with the flask db
downgrade command (note that this may cause some data to be
lost).