This project includes the basic boilerplate required to integrate SQLAlchemy with Flask. Alembic database upgrading is included. Flask-SQLAlchemy extension is not used: Among other things, a custom base class for SQLAlchemy declarative models that creates a dependency on Flask is therefore not required.
These instructions may be Ubuntu linux-specific in some places. Python 3.6+ and pipenv are required.
- create
env.sh
file and add the following environment variables (substitute~/dev/flask_sqlalchemy_starter
,username
,userpassword
,server
, anddbname
accordingly):
export PYTHONPATH=~/dev/flask_sqlalchemy_starter
export SQLALCHEMY_USERNAME=username
export SQLALCHEMY_PASSWORD=userpassword
export SQLALCHEMY_SERVER=server
export SQLALCHEMY_DBNAME=dbname
export SQLALCHEMY_URL="postgresql://$SQLALCHEMY_USERNAME:$SQLALCHEMY_PASSWORD@$SQLALCHEMY_SERVER/$SQLALCHEMY_DBNAME"
You don't have to use postgresql as the db of course.
- If you wish to use slite in memory, set SQLALCHEMY_URL to
"sqlite://"
(see env_test.sh)- If you want to use postgresql and it's not installed on your system yet, I've included instructions for postgresql installation on Ubuntu linux at the end of this file.
- start python virtual environment:
$ pipenv shell
- install:
$ pipenv install
- install with dev dependencies (unit testing):
$ pipenv install --dev
- run:
$ source env.sh && FLASK_ENV=development flask run
http://localhost:5000/
should show json greetinghttp://localhost:5000/messages
should show json list of messages
- run tests:
$ source env_test.sh
followed by$ pytest -v
$ source env.sh
$ ./create_db.sh
cd flask_sqlalchemy_starter && alembic upgrade head && cd ..
$ psql -a -f populate_db.sql
(pre-populates db with a few rows of data)
$ echo "deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
'bionic' in the above command refers to the codename for ubuntu linux 18.04. Update as required for appropriate linux version
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get install postgresql-10 pgadmin4
You can omit pgadmin4 if you don't want to use it.
Create a db user corresponding to your linux username and give that user a db password (not the same as your linux password):
$ sudo -u postgres createuser -s "$SQLALCHEMY_USERNAME"
$ psql -a -f update_user_password.sql