pg_search icon indicating copy to clipboard operation
pg_search copied to clipboard

Perhaps you meant to reference the table alias

Open afuno opened this issue 6 years ago • 1 comments

I have the titles and descriptions that are implemented through the STI:

class Content < ApplicationRecord
  # ...
class Title < Content
  #...
class Description < Content
  # ...

Titles and Descriptions are used in Posts:

class Post < ApplicationRecord
  has_many :titles,
           as: :resource,
           dependent: :destroy

  has_many :descriptions,
           as: :resource,
           dependent: :destroy

  # ...

Next, I try to connect your library:

class Post < ApplicationRecord
  include PgSearch::Model

  pg_search_scope :search_by_query,
                  associated_against: {
                    titles: %i[value]
                    # descriptions: %i[value]
                  },
                  using: {
                    tsearch: {
                      dictionary: :simple,
                      prefix: true
                    }
                  }

  has_many :titles,
           as: :resource,
           dependent: :destroy

  has_many :descriptions,
           as: :resource,
           dependent: :destroy

  # ...

As a result, I get this error:

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: invalid reference to FROM-clause entry for table "contents") LINE 1: ...arch_000f14e102512801f60bcb.pg_search_id ORDER BY contents.v...

HINT: Perhaps you meant to reference the table alias "titles_posts".

What is the problem? How to fix it?

afuno avatar Aug 02 '19 12:08 afuno

This looks like a legitimate bug. We are probably not generating the SQL statement correctly for this STI sort of situation where there are multiple associations to the same underlying table.

The best way to approach this would be to add a test case that reproduces the issue and then use that to iterate on different solutions until we get everything passing again. I don't get a lot of time to work on pg_search so pull requests are always excellent. But if I do get a free moment I'll try to look into this.

Sorry for the trouble!

nertzy avatar Aug 02 '19 15:08 nertzy