def get_model_by_tablename(origin, table):
    """Return the model that represents given table."""
    for model in origin._decl_class_registry.values():
        if hasattr(model, '__tablename__') and model.__tablename__ == table:
            return model
    raise ValueError('No model exists for table %s' % table)