Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at or near "AS" #199

Closed
homanchou opened this issue Nov 10, 2014 · 3 comments

Comments

@homanchou
Copy link

Upgraded from pg_search 0.7 and rails 4.0.5 to pg_search 0.7.8 and rails 4.2.beta4

Now getting this error:

ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR: syntax error at or near "AS"

The old query from 0.7 looked like:

SELECT COUNT(*) FROM "merchants" WHERE (((setweight(to_tsvector('simple', coalesce("merchants"."email"::text, '')), 'A') || setweight(to_tsvector('simple', coalesce("merchants"."company_name"::text, '')), 'B') || setweight(to_tsvector('simple', coalesce("merchants"."first_name"::text, '')), 'C') || setweight(to_tsvector('simple', coalesce("merchants"."last_name"::text, '')), 'D')) @@ (to_tsquery('simple', ''' ' || 'test@sellbrite.com' || ' ''' || ':*'))) OR ((coalesce("merchants"."email"::text, '') || ' ' || coalesce("merchants"."company_name"::text, '') || ' ' || coalesce("merchants"."first_name"::text, '') || ' ' || coalesce("merchants"."last_name"::text, '')) % 'test@sellbrite.com')) AND "merchants"."state" IN ('subscribed', 'in_trial', 'tester', 'trial_ended', 'canceled', 'suspended') AND (sign_in_count > 0)

new query from 0.7.8 looks like

SELECT COUNT("merchants".*, ((ts_rank((setweight(to_tsvector('simple', coalesce("merchants"."email"::text, '')), 'A') || setweight(to_tsvector('simple', coalesce("merchants"."company_name"::text, '')), 'B') || setweight(to_tsvector('simple', coalesce("merchants"."first_name"::text, '')), 'C') || setweight(to_tsvector('simple', coalesce("merchants"."last_name"::text, '')), 'D')), (to_tsquery('simple', ''' ' || 'test@sellbrite.com' || ' ''')), 0))) AS pg_search_rank) FROM "merchants" WHERE (((setweight(to_tsvector('simple', coalesce("merchants"."email"::text, '')), 'A') || setweight(to_tsvector('simple', coalesce("merchants"."company_name"::text, '')), 'B') || setweight(to_tsvector('simple', coalesce("merchants"."first_name"::text, '')), 'C') || setweight(to_tsvector('simple', coalesce("merchants"."last_name"::text, '')), 'D')) @@ (to_tsquery('simple', ''' ' || 'test@sellbrite.com' || ' ''')))) AND "merchants"."state" IN ('subscribed', 'in_trial', 'tester', 'trial_ended', 'canceled', 'suspended') AND (sign_in_count > 0))

My model looks like:

class Merchant < ActiveRecord::Base

    include PgSearch

   #creates a search method on the product model
  pg_search_scope :search, against: [
     [:email, 'A'],
     [:company_name, 'B'],
     [:first_name, 'C'],
     [:last_name, 'D'],
  ], :using => {
                  :tsearch => {:prefix => true},
                  :trigram => {}
                }
psql --version
psql (PostgreSQL) 9.3.2

Is this a pg_search bug or an activerecord 4.2 bug?

@DylanGriffith
Copy link

I'm also seeing this issue with:

     Failure/Error: match = Message.search_content("hello").count
     ActiveRecord::StatementInvalid:
       PG::SyntaxError: ERROR:  syntax error at or near "AS"
       LINE 1: ...query('simple', ''' ' || 'hello' || ' ''')), 0))) AS pg_sear...
                                                                    ^
       : SELECT COUNT("messages".*, ((ts_rank((to_tsvector('simple', coalesce("messages"."content"::text, ''))), (to_tsquery('simple', ''' ' || 'hello' || ' ''')), 0))) AS pg_search_rank) FROM "messages"  WHERE (((to_tsvector('simple', coalesce("messages"."content"::text, ''))) @@ (to_tsquery('simple', ''' ' || 'hello' || ' '''))))

Model:

class Message < ActiveRecord::Base
  include PgSearch
  belongs_to :channel
  pg_search_scope :search, :against => :content
end 
$ psql --version                                                                                                                                                                                                    
psql (PostgreSQL) 9.3.4
$ rails --version
Rails 4.1.7

@DylanGriffith
Copy link

Also for me the problem only occured when using count. Otherwise enumerating the results actually worked fine. It looks like you're using count too. In your 2 examples it looks like there is a difference between the way it is constructing a COUNT query.

@nertzy
Copy link
Collaborator

nertzy commented Nov 20, 2014

Looks like you're having the same problem as #127, #153, and #180.

Due to a change in Active Record (See rails/rails#10710) you need to pass :all to count.

MyModel.my_scope.count(:all)

Please let me know whether this works for you.

@nertzy nertzy closed this as completed Nov 20, 2014
whilei added a commit to rotblauer/openbound that referenced this issue Mar 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants