Flask and SQL Alchemy Everything You Need To Know

SQL stands for structured query language and is used to communicate with a database. It is used to update and retrieve data from a database.

A database is used to store.. well.. data. Databases have three elements which are Tables, Records and Fields. A “Table” is a collection of a particular type of data. This could be “Users” or “Customers”. Records are also called rows because they basically represent one row in the table. A field is a part of the record and is a single piece of data.

Difference between Non-Relational and Relational database

Relational databases store data in tables are very structured and can be connected together while Non-Relational databases are like documents and store data as categories.

Working with Non-Relational databases is a lot more flexible and inspires speed and agility. While working with Relational databases is simpler because with just some basic SQL queries you can easily access data. All data should respond to clear regulations that are the same for all DBMS while working with Relational Databases. This unfortunately means the initialization takes a lot longer. But this also means they are a lot more structured.

Examples of Relational Database: PostgreSQL, MySQL

Examples of Non-Relational Database: MongoDB, Cassandra

What is SQL Alchemy?

Lets look at a simple SQL Query:-

SELECT * FROM Customers WHERE email = '[email protected]';

If you’ve worked with SQL before this is a very simple query and quite easy to understand. However using ORM you can convert this into a object oriented approach of your programing language which in this case is python.

This is how the query would look like in python using SQL Alchemy:-

session.query.filter(Customers.email == '[email protected]')

According to sqlalchemy.org:- A successful application may be constructed using the Object Relational Mapper exclusively. In advanced situations, an application constructed with the ORM may make occasional usage of the Expression Language directly in certain areas where specific database interactions are required.

Its always a good idea to learn a bit about some SQL commands get rid of the initial confusion and familiarize yourself with the direct process. W3schools has a great series which goes in depth about SQL here.

Resources

What is Flask-SQLAlchemy?

If you dig into Flask SQLAlchemy without much knowledge of working with ORM like it did you might notice even if it feels easy in the beginning soon enough you start having issues which take ages to resolve. This is why I’d recommend you to start from SQL move to SQL Alchemy and then Flask-SQLAlchemy will be a breeze.

Other than few initialization requirements there’s not much of a difference here which is a good thing because there’s a lot more material available for SQLAlchemy which should make it very easy to solve most of your issues.

This section from the official site for Flask-SQLAlchemy makes things clear about everything you really need to know while using this extension.

Road to Enlightenment

The only things you need to know compared to plain SQLAlchemy are:

  1. SQLAlchemy gives you access to the following things:
    • all the functions and classes from sqlalchemy and sqlalchemy.orm
    • a preconfigured scoped session called session
    • the metadata
    • the engine
    • a SQLAlchemy.create_all() and SQLAlchemy.drop_all() methods to create and drop tables according to the models.
    • a Model baseclass that is a configured declarative base.
  2. The Model declarative base class behaves like a regular Python class but has a query attribute attached that can be used to query the model. (Model and BaseQuery)
  3. You have to commit the session, but you don’t have to remove it at the end of the request, Flask-SQLAlchemy does that for you.

Leave a Comment