Error group of PostgreSQL grouping by table that exists only there

This is a strange problem. I use PG search as well as some home classes to populate the search results on my site. Home classes are tags to further refine your search. For example, you are looking for products, and you want organic ones to narrow down your search results to those who are not tagged organic and food. This worked well for quite a while.

It all started after I upgraded to 4.2.1 rails.

I was able to trace the error to a specific line of code:

 joins(:tags).where(tags: {id: tag_list }).group("businesses.id").having("count(*) 
 = #{tag_list.size}")

This line of code is part of the following function:

  def self.tagged_with_all(tag_list)
   unless tag_list.empty?
     joins(:tags).where(tags: {id: tag_list }).group("businesses.id").having("count(*) = #{tag_list.size}")
  else
   all
  end
 end

The error looks like this:

PG::GroupingError: ERROR:  column "pg_search_businesses.rank" must 
appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...UP BY businesses.id HAVING count(*) = 1  ORDER BY pg_search_...

So, I thought, why not just follow the error recommendations and add pg_search_business.rank to the aggregate, so the complex line at the top would be:

 joins(:tags).where(tags: {id: tag_list }).group("businesses.id", 
 "pg_search_business.rank").having("count(*) 
 = #{tag_list.size}")

, pg_search_business.rank . , , :

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table 
"pg_search_businesses"LINE 1: ...."name" = 'Kid Friendly') 
GROUP BY businesses.id, pg_search_

, pg_search_business.rank RoR .

, self.tagged_with_all pg_search.

, , - , pg_search_businesses.rank, - , . .

. , , , , .

+4
1

@mu

, . , pg_search_businesses.rank .

$group_text

    unless q.empty?
     $group_text = "businesses.id" + "," + " pg_search_businesses.rank"
    else
     $group_text = "businesses.id"
    end

, ( , q .

  def self.search(q)
    unless q.empty?
     $group_text = "businesses.id" + "," + " pg_search_businesses.rank"
    else
     $group_text = "businesses.id"
    end
    unless q.empty?
      search_text(q)
    else
      all
    end
  end

, , , .

SO mu

0

Source: https://habr.com/ru/post/1606857/


All Articles