Various flask/sqlalchemy compat fixes