active_record_union icon indicating copy to clipboard operation
active_record_union copied to clipboard

PG::SyntaxError: ERROR: syntax error at or near "desc"

Open khiav223577 opened this issue 4 years ago • 0 comments

I have a model with a column named desc.

It works fine when using pluck

Mission.where(id: 1).union(id: 2).pluck('desc')
# SELECT "missions"."desc" FROM ( (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $1) UNION (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $2) ) "missions"  [["id", 1], ["id", 2]]

But it is broken when using select

Mission.where(id: 1).union(id: 2).select('desc')
# SELECT  desc FROM ( (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $1) UNION (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $2) ) "missions" LIMIT $3  [["id", 1], ["id", 2], ["LIMIT", 11]]

# ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near "desc")
# LINE 1: SELECT  desc FROM ( (SELECT "missions".* FROM "missions" WHE...
#                 ^
# : SELECT  desc FROM ( (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $1) # UNION (SELECT "missions".* FROM "missions" WHERE "missions"."id" = $2) ) "missions" # LIMIT $3

It seems that it fails to quote the column desc as "missions"."desc"

Environment

Rails version: 5.2.1 ruby version: 2.5.1 active_record_union: 1.3.0

khiav223577 avatar Sep 18 '19 11:09 khiav223577