Fix sqlalchemy.exceptions bug