简体   繁体   中英

Heroku Postgres Error: Column does not exist

I'm getting the following error after I deploy to heroku (running PG), though it works fine in local (running sqlite3)

ActionView::Template::Error (PGError: ERROR:  column "calendar_day" does not exist
LINE 1: ...ated_at > '2012-04-09 19:39:54.787077') AND (date(calendar_d...
                                                         ^
SELECT  date(created_at) as calendar_day, count(*) as total_checkins FROM "checkins"  WHERE (user_id = 1 AND group_id = 1 AND created_at > '2012-04-09 19:39:54.787077') AND (date(calendar_day) = '2012-05-07') GROUP BY date(created_at) LIMIT 1):
...
app/helpers/groups_helper.rb:32:in `checkin_day'

Note that "calendar_day" is not an actual column on a persisted table, but one I define on the fly using an AS statement (not sure what this is technically called). The relevant helper functions are shown below. Basically, at the top of the page I make a call to calendar_view, later I pass the results to checkin_day as the checkins parameter. It's on this checkin_day call that I hit the error. Any idea why this works fine in local but not in heroku?

def calendar_view(member, timeperiod)
  Checkin.select("date(created_at) as calendar_day, count(*) as total_checkins").where("user_id = ? AND group_id = ? AND created_at > ?", member.member.id, member.group.id, Time.now - timeperiod.days).group("date(created_at)")
end

def checkin_day(checkins, day)
  ch = checkins.where("date(calendar_day) = ?", day).first
  return ch.total_checkins if ch
  return 0
end

Stuff you define on the SELECT part of the query (projection) is not available during the evaluation of the WHERE clause (selection). You have to define it again there:

def checkin_day(checkins, day)
  ch = checkins.where("date(created_at) = ?", day).first
  return ch.total_checkins if ch
  return 0
end

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