简体   繁体   中英

Heroku PG error on select

I have a piece of code working great in dev environment and firing an error in production. Here it is:

venue.badges.where(:active => true).select([:title, :desc, :bonus]).each do |badge|

While working on MySQL local DB, it fires this in production:

ActiveRecord::StatementInvalid (PG::Error: ERROR:  syntax error at or near "desc"
: SELECT title, desc, bonus FROM "badges"  WHERE "badges"."venue_id" = 22 AND "badges"."active" = 't'):
LINE 1: SELECT title, desc, bonus FROM "badges"  WHERE "badges"."ven...

I really don't get what's wrong here.

DESC is a reserved keyword in SQL. This list of SQL and PostgreSQL keywords is a good reference. To use a reserved word as a column name you need escape it with double quotes:

SELECT title, "desc", bonus FROM badges WHERE ...

And in rails, pass it as a literal string rather than a symbol:

venue.badges.where(:active => true).select([:title, '"desc"', :bonus]).each do |badge|

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM