Refactor for Flask-SQLAlchemy