def ensure_join(qs, lm, rm, on=None, **join_kwargs):
    """TODO: remove me when problem in Peewee will be fixed."""
    ctx = qs._query_ctx
    for join in qs._joins.get(lm, []):
        if join.dest is rm:
            return qs
    return qs.switch(lm).join(rm, on=on, **join_kwargs).switch(ctx)
