Install SQLAlchemy to Flask 1.0.2 & Python 3
Installing SQLAlchemy to Flask 1.0.2 can be intimidating for beginner as it’s difficult to find the tutorial how to solve this without assumption we have understand other related parts and some other tutorials are outdated. This article will provide how to install SQLAlchemy to Flask 1.0.2 as simple as possible.
First, We’ll create a simple flask application by following this tutorial to set up your local environment for flask: http://flask.pocoo.org/docs/1.0/installation/#install-flask. After that let’s install SQLAlchemy (makesure you’re still in virtual environment venv):
$ pip install SQLAlchemy
Next we need to install flask-migrate to implement database migration:
$ pip install Flask-Migrate
We also need mysqlclient to allow flask connect to MySQL:
$ pip install mysqlclient
Cool, now let’s a file named db.py in root project directory as the model then execute migration from the model:
Export the file to flask as follow:
$ export FLASK_APP=db.py
Generate migration repository to our project:
$ flask db init
Then generate initial migration:
$ flask db migrate
Last, apply migration to the database:
$ flask db upgrade
As you can see, now in my database I’ve got a new table named “log” . Installing SQLAlchemy to Flask 1.0.2 is simple however if we’re using different version or missing certain step it’ll make this process kinda frustrating. Hope this article will help you to set up SQLAlchemy to your Flask project.