Tweaked to use relationship rather than sqlalchemy query