Based on https://pythonhosted.org/Flask-SQLAlchemy/