Set the where clause for the relation query.

        :return: self
        :rtype: BelongsToMany
14119
def _set_where(self):
        
        foreign = self.get_foreign_key()

        self._query.where(foreign, "=", self._parent.get_key())

        return self
1164793
def or_where(self, key, operator, value):
        
        if len(self._queries) > 0:
            self._current_query_index += 1
        self.__store_query({"key": key, "operator": operator, "value": value})
        return self
1027332
def get_relation_count_query(self, query, parent):
        
        if parent.get_query().from__ == query.get_query().from__:
            return self.get_relation_count_query_for_self_join(query, parent)

        self._set_join(query)

        return super(BelongsToMany, self).get_relation_count_query(query, parent)
822583
def _set_join(self, query=None):
        
        if not query:
            query = self._query

        base_table = self._related.get_table()

        key = '%s.%s' % (base_table, self._related.get_key_name())

        query.join(self._table, key, '=', self.get_other_key())

        return self
570871
function query (args) {
    // get relation
    var relation = this.model.relation(args.relation)
    // delete relation name from args so it wont pass to query
    delete args.relation
    // build query args
    var queryArgs = {
        session: args.session || this.session,
        where: { relation: { name: this.model.name } },
    }
    // relation is via a linking table
    if (relation.via) {
        // query by current records ids
        queryArgs.where.relation[relation.viaModelIdColumn] = this[this.model.defaultColumns[relation.modelIdColumn]]
    }
    // relation is direct
    else {
        // query by current records ids
        queryArgs.where.relation[relation.relationIdColumn] = this[this.model.defaultColumns[relation.modelIdColumn]]
    }
    // merge args
    _.merge(queryArgs, args)
    // do query on related model
    return relation.model.query(queryArgs)
}
1147701
def apply_scopes(self):
        
        if not self._scopes:
            return self

        builder = copy.copy(self)

        query = builder.get_query()

        # We will keep track of how many wheres are on the query before running the
        # scope so that we can properly group the added scope constraints in the
        # query as their own isolated nested where statement and avoid issues.
        original_where_count = len(query.wheres)

        where_counts = [0, original_where_count]

        for scope in self._scopes.values():
            self._apply_scope(scope, builder)

            # Again, we will keep track of the count each time we add where clauses so that
            # we will properly isolate each set of scope constraints inside of their own
            # nested where clause to avoid any conflicts or issues with logical order.
            where_counts.append(len(query.wheres))

        if self._should_nest_wheres_for_scope(query, original_where_count):
            self._nest_wheres_for_scope(query, Collection(where_counts).unique().all())

        return builder
169510
def add_eager_constraints(self, models):
        
        table = self._parent.get_table()

        self._query.where_in("%s.%s" % (table, self._first_key), self.get_keys(models))
434172
def where(self, *args):
        ''' 
        '''
        if not self._table:
            raise errors.NoTableSelectedError('No Table Selected')

        clause = []
        self._query += ' WHERE '

        clause = [ self._clause_formatter(x) for x in args if x ]

        self._query += ' AND '.join(clause)

        payload = self._payload_builder(self._query)
        response = self.execute_query(payload)

        return response
1224391
def match(self, models, results, relation):
        
        dictionary = self._build_dictionary(results)

        for model in models:
            key = model.get_key()

            if key in dictionary:
                collection = self._related.new_collection(dictionary[key])

                model.set_relation(relation, collection)

        return models
1179692
def where_before_entry(query, ref):
    
    return orm.select(
        e for e in query
        if e.local_date < ref.local_date or
        (e.local_date == ref.local_date and e.id < ref.id)
    )
723674
def _set_keys_for_save_query(self, query):
        
        query.where(self.__foreign_key, self.get_attribute(self.__foreign_key))

        return query.where(self.__other_key, self.get_attribute(self.__other_key))
1132955
def query(self, *q):
    
    params = join_params(self.parameters, {"q": q})

    return self.__class__(**params)
1260905
def _iter_where_in(self, tree):
        
        desc = self.describe(tree.table, require=True)
        for keypair in tree.keys_in:
            yield desc.primary_key(*map(resolve, keypair))
1236870
def get_relation_count_query_for_self_join(self, query, parent):
        
        query.select(QueryExpression('COUNT(*)'))

        table_prefix = self._query.get_query().get_connection().get_table_prefix()

        hash_ = self.get_relation_count_hash()
        query.from_('%s AS %s%s' % (self._table, table_prefix, hash_))

        key = self.wrap(self.get_qualified_parent_key_name())

        return query.where('%s.%s' % (hash_, self._foreign_key), '=', QueryExpression(key))
259142
def add_eager_constraints(self, models):
        
        return self._query.where_in(
            self._foreign_key, self.get_keys(models, self._local_key)
        )
790617
def where_clause(self):
        
        cond = self._make_condition(self.restriction)
        return '' if cond is True else ' WHERE %s' % cond
672674
def _extract_where(cls, queryset):
        
        if not cls._is_simple_query(queryset.query):
            raise ValueError("This QuerySet's WHERE clause is too complex to "
                             "be used in a HANDLER")

        sql, params = queryset.query.sql_with_params()
        where_pos = sql.find('WHERE ')
        if where_pos != -1:
            # Cut the query to extract just its WHERE clause
            where_clause = sql[where_pos:]
            # Replace absolute table.column references with relative ones
            # since that is all HANDLER can work with
            # This is a bit flakey - if you inserted extra SQL with extra() or
            # an expression or something it might break.
            where_clause, _ = cls.absolute_col_re.subn(r"\1", where_clause)
            return (where_clause, params)
        else:
            return ("", ())