Reader small image

You're reading from  Full-Stack Flask and React

Product typeBook
Published inOct 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781803248448
Edition1st Edition
Right arrow
Author (1)
Olatunde Adedeji
Olatunde Adedeji
author image
Olatunde Adedeji

Olatunde Adedeji is a seasoned web developer with over 15 years of experience in developing dynamic and detail-oriented enterprise web applications. Along with his extensive experience in developing, maintaining and supporting mobile, web and enterprise applications in Python, Go and JavaScript, Olatunde has consistently delivered excellent services, as team lead, team member or in consultancy capacity. Olatunde is proficient in application design and solution blueprinting; including mastery of application development tools such as: Flask, Django, Gin, React, and NodeJS. When not weaving the web and playing chess, Olatunde spends time with his wife, Adedoyin and two daughters, Mitchelle and Mabel.
Read more about Olatunde Adedeji

Right arrow

SQL and Data Modeling

So far, we’ve explored React, a key library in frontend technology stacks. Now, we will explore the world of backend development, starting with Structured Query Language (SQL) and data modeling.

SQL and data modeling are critical skills for any backend developer, and starting with these skills in the backend development journey will give you a strong foundation to build robust and scalable web applications. SQL is a standard language used to manage data in relational databases, which are essentially used to store structured data.

Knowledge of SQL will help you to write optimized SQL queries that can improve the performance of your application and reduce server load. Data modeling will help you to design a database schema that reflects the data your application will work with. Data modeling can help you avoid performance issues, maintainability problems, and other common issues that may arise when working with databases.

We will dive deeply into...

Technical requirements

The complete code for this chapter is available on GitHub at: https://github.com/PacktPublishing/Full-Stack-Flask-and-React/tree/main/Chapter08.

What is the relational data model?

The relational data model is a conceptual approach used to represent a database as a group of relations. Most web applications are highly data-driven. Developers have to deal with either code-level data storage, in the case of a data structure, or find a way to persistently store data in an RDBMS, such as PostgreSQL or MySQL.

In an RDBMS, you can refer to a table as a relation. Therefore, a relational model represents data as a collection of relations or tables. Breaking down the database structure further, you then have rows and columns making up a table. Then, you have a record, which consists of a combination of rows and columns.

Let’s take a look at a hypothetical table named customers that depicts the structure of a typical table for clarity:

Exploring the different database relationships

In the client-server model, the database resides at the server end of the infrastructure. The database is core to any production-grade web application in collecting and storing application data. Understanding relationships that exist in a database is vital for organizing, managing, and retrieving useful data from a database.

As previously mentioned, there are three types of relationships that exist in a database – one-to-one, one-to-many, and many-to-many. We will begin by delving into the concept of a one-to-one relationship.

One-to-one (1:1) relationship

A one-to-one relationship in a data model refers to a direct link relationship that exists in information between two tables. With a one-to-one relationship, you have a situation where a record in one table is directly associated with a specific row in another table. For clarity, let’s quickly dive into a scenario where you have two tables – speakers and...

Setting up PostgreSQL, SQLAlchemy, and Alembic

We are going to start by setting up the database tools needed for the backend database. PostgreSQL is a popular free and open source RDBMS. It is similar to other dialects of the SQL databases that exist – for example, MySQL, MariaDB, and Oracle. This database can be used to store data for any web application. PostgreSQL has enterprise-grade features that make it robust, scalable, and reliable.

We will also set up SQLAlchemy, an object-relational mapper (ORM). An ORM is a high-level abstraction layer on top of a relational database that allows web developers to rely on Python object-oriented programming code to execute database operations, such as read, insert, update, and delete, rather than writing SQL queries directly. Finally, in this section, we will set up Alembic to handle database migrations.

Setting up PostgreSQL

To get started with PostgreSQL locally on your machine, download it from https://www.postgresql.org...

Understanding database concepts for Flask applications

Now that we have set up our database and connected with it using the Terminal, it is crucial to have a solid understanding of some database concepts to be able to set up backend services that can collect, store, and retrieve users’ data.

Most modern web applications have a database to store users’ data. As a full stack web developer, part of your responsibility is to be able to set up backend services that can collect, store, and retrieve users’ data. We will dive into interacting with databases from a Flask application shortly, but before that there are few database concepts you need to understand.

Let’s take a quick overview of the following database concepts.

RDBMS

When working with a database in a production environment, you need an RDBMS. An RDBMS is a software package that allows you to interact with a database. The RDBMS software allows you to define, manipulate, retrieve, and manage...

Understanding SQLAlchemy ORM basics

SQLAlchemy offers developers the ability to work entirely in Python code to create, read, update, and delete tables. SQLAlchemy is the Python SQL toolkit and ORM that allows application developers to interact with databases without writing direct SQL statements.

As SQLAlchemy is an ORM library, Python classes are mapped to tables, and the instances of those classes are mapped to table rows in a relational database. You then have a situation where you can use Python object-oriented programming code to perform database SQL create, read, update, and delete (CRUD) operations and other necessary operations in your applications.

The ORM feature of SQLAlchemy gives Python developers the power to harness function calls to generate SQL statements out of the box. With this new way of thinking about databases, developers are able to decouple object models and database schema, leading to a more flexible database structure, elegant SQL statements, and Python...

Modeling data for a speakers’ conference web application

Data modeling is the process of creating a conceptual, logical, and visual representation of data structures and relationships between data elements, in order to understand, analyze, and manage complex information systems. Data modeling involves identifying entities (objects, concepts, or things) that will be represented in a system and defining their attributes and relationships with other entities.

The main purpose of data modeling is to create a clear and precise representation of the data that will be stored, processed, and managed by an information system. A well-designed data model in a web application can ensure that the web application is scalable, efficient, and meets the needs of its users.

Let’s quickly examine some of the best practices to be considered when designing a data model for the conference speakers’ web application:

  • Identifying the entities: Start by identifying the entities...

Sending data to the PostgreSQL database from a Flask app

Interacting with a database is a common aspect of most web applications. Sending data to the PostgreSQL database from a Flask app is simple and straightforward. A web application would not be complete without the ability to store and retrieve data from a database.

The first step in sending data to a database is to ensure there is a connection between the Flask app and the database. This involves installing the required libraries and ensuring that you work in a virtual environment to contain your installation and prevent unexpected occurrences, due to the interference of other libraries.

Let’s make use of the bizza/backend directory created in the Setting up the development environment with Flask section of Chapter 1. You can create one if you haven’t already. Install and activate the virtual environment.

To create a virtual environment, open your project root directory in a terminal and add the following...

Migration with Alembic

As discussed earlier, Alembic is a migration tool that makes the tracking of changes in a database a less problematic operation for Flask developers. Since we expect data models to change, we need a tool that can keep track of these changes and ensure they are updated in the database.

This is similar to how we do version control of source code using Git. The same applies to database schema change management, where we keep incremental and reversible changes in the database structure. Working with a database table, you will want to add or remove columns, thus altering the schema in a Python model class.

Once this is done, you need an automatic process to ensure your database table and the state of the data model schema are in sync. Alembic graciously handles the schema migration and ensures that the data model in a Python file is the same as the database structure.

Let’s examine how you can implement migration in a Flask application. We will add...

Summary

In this chapter, we extensively discussed SQL and relational data modeling for the web. A relational database helps us with the design of a database as a group of relations. We also discussed relationships that can exist in a database, such as one-to-one, one-to-many, and many-to-many relationships, allowing us to logically group relations in the database and enforce data referential integrity.

Additionally, we examined how to set up PostgreSQL. We shed light on the basics of SQLAlchemy and its associated database adapters and how they are used in Flask application development. We discussed data model design, with the Bizza project as a use case. Finally, we discussed how a Flask app can communicate with the database and migration in Flask to keep track of changes in a database.

In the next chapter, we will extensively discuss the API in backend development and how you can use the Flask framework to implement API design.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Full-Stack Flask and React
Published in: Oct 2023Publisher: PacktISBN-13: 9781803248448
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Olatunde Adedeji

Olatunde Adedeji is a seasoned web developer with over 15 years of experience in developing dynamic and detail-oriented enterprise web applications. Along with his extensive experience in developing, maintaining and supporting mobile, web and enterprise applications in Python, Go and JavaScript, Olatunde has consistently delivered excellent services, as team lead, team member or in consultancy capacity. Olatunde is proficient in application design and solution blueprinting; including mastery of application development tools such as: Flask, Django, Gin, React, and NodeJS. When not weaving the web and playing chess, Olatunde spends time with his wife, Adedoyin and two daughters, Mitchelle and Mabel.
Read more about Olatunde Adedeji

Id

firstname

lastname

email

phone

...