How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

June 10, 2018
How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

“How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS” covers the process of creating a user model in a Flask application. The user model we are creating will store information that users will use to login to our application. Individual user emails, usernames, passwords, and administrator status will be stored in the user model in addition to a unique id number. This tutorial assumes that your Flask application is written and configured as in previous tutorials. If your Flask app is not configured in this manner, please follow those tutorials before continuing with this one.

1. Launch VSCode and open your "first_flask_app" project folder.

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

2. Select the Terminal tab from the pane on the bottom of VSCode and start the virtual environment we configured by entering the following into the terminal:

source activate flask-env

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

3. Start our application by entering the following into the terminal:

python main.py

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

4. You should receive an error-free output like this:

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

5. Open your web browser and navigate enter the following address into the address field to view your Flask application:

http://127.0.0.1:5000

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

6. Return to VSCode so that we can create our user model. The first step to creating our model is to create a User class. Add the following code to main.py file for your Flask application:

class User(db.Model):

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

7. Next we create the structure of what will become the user table in our first_flask_app database. We will need five columns for our table: an id, email, username, password, and admin status column. In the main.py file for your Flask app, enter the following code under the User class we created:


id = db.Column(db.Integer, primary_key=True) 
email = db.Column(db.String(75)) 
username = db.Column(db.String(50)) 
password = db.Column(db.String(100)) 
is_admin = db.Column(db.String, default=False)

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

8. Now we can connect our model to our SQL database. Stop your Flask application by entering CTRL + C in your terminal. Then start python in the terminal by entering the following into the terminal and pressing the Enter key on your keyboard:

python

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

9. Enter the following into the terminal to import your model:

from main import db, User

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

10. Next, enter the following into the terminal to create our table and its configuration:

db.create_all()

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

11. The output of the previous command should show our table configuration as in the following screenshot:

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

12. We must now commit our table and configuration to the first_flask_app database by entering the following into the terminal and pressing Enter on your keyboard:

db.session.commit()

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

13. We are now finished using the Python command line interface and can close it by entering the following into the terminal and pressing Enter on your keyboard:

exit()

How to Create a SQLAlchemy Database Model for a Flask Application on Ubuntu 18.04 LTS

14. After exiting the Python command line interface, the creation, configuration, and connection of our SQLAlchemy User model to our MySQL database “first_flask_app” is complete. This means that we are now able to save user information indefinitely and can use this information to authenticate users to control what routes in our Flask application they can view.