【问题标题】:Heroku - ActiveRecord::StatementInvalid (PG::Error: ERROR: column "requested" does not existHeroku - ActiveRecord::StatementInvalid (PG::Error: ERROR: "requested" 列不存在
【发布时间】:2013-03-25 18:54:55
【问题描述】:

我有一个absense.html.erb,它在开发和生产中运行良好。但是,当我尝试访问该页面时,出现以下错误:

2013-03-25T18:43:43+00:00 app[web.1]: 开始 GET "/absence" for 77.100.90.77 在 2013-03-25 18:43:43 +0000 2013-03-25T18:43:43+00:00 应用[web.1]: 2013-03-25T18:43:43+00:00 应用[网络.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR: column "requested" 不存在 2013-03-25T18:43:43+00:00 app[web.1]: LINE 1: ...LECT “假期”。*来自“假期”WHERE(状态=“请求... 2013-03-25T18:43:43+00:00 应用[web.1]:
^ 2013-03-25T18:43:43+00:00 app[web.1]: : SELECT "holidays".* FROM “假期”在哪里(状态 = “请求”)):2013-03-25T18:43:43+00:00 应用[web.1]:2013-03-25T18:43:43+00:00 应用[web.1]: 2013-03-25T18:43:43+00:00 应用[web.1]:
app/controllers/holidays_controller.rb:52:in `absence' 2013-03-25T18:43:43+00:00 heroku[路由器]: at=info 方法=GET 路径=/rota_days 主机=miuk-portal.herokuapp.com fwd="77.100.90.77" dyno=web.1 队列=0 等待=0ms 连接=1ms 服务=5773ms 状态=200 字节=897173

错误指向我的控制器中我的缺席方法的以下行

def absence
#show the holidays where the approver id matches the current user id
#and state = "requested"'

    @user = current_user
    if current_user.role? :administrator
      # a superadmin can view all current holiday requests
      @holidays = Holiday.find(:all, :conditions => 'state = "requested"')
    else
      #otherwise an admin sees the holiday requests that they are approvers for
      @holidays = Holiday.find(:all, :conditions => ["approver_id = #{current_user.id}", "state = requested"])
    end
  end

我已经在我的假期模型中声明了这一点,如下所示:

Holiday.rb

class Holiday < ActiveRecord::Base

  belongs_to :user
  belongs_to :calendar
  belongs_to :type, :class_name => "Type"
  belongs_to :approver, :class_name => "User"


  before_create :default_values #Before creating holiday set default_values
                                #before_create :overflow


  validates :start_at, :presence => { :message => "must be a valid date/time" }
  validates :end_at, :presence => {:message => "must be a valid date/time"}
  validate :start_must_be_before_end_date
  validate :overflow #Hook method to determine if user holidays are over the set amount
  validates_presence_of :end_at, :start_at

  attr_accessible :description, :end_at, :start_at, :state, :type_id, :user_id, :color


  def length
    (self.end_at.to_i - self.start_at.to_i)/(60*60*24)
  end

  def days_used
    ( (start_at.to_date)..(end_at.to_date) ).select {|d| (1..5).include?(d.wday) }.size
  end


  #Validates_presence_of is called to ensure that the start date exisit.
  #Start date must be lt or = to end date otherwise throw error.
  def start_must_be_before_end_date
    errors.add(:start_at, "must be before end date") unless
        self.start_at <= self.end_at
  end

  #Overflow of holidays is validated by calling "validates" L31. Then checks if absent days
  #Is gt or = to length
  def overflow
    errors.add(:overflow, "- You only have #{user.absentdays} days holiday remaining; this absence is #{length} days.") unless
        user.absentdays >= length
  end

  def name
    return self.user.name
  end

  def colors
    if state ||= "denied"
      return self.color ||= "#C00000"

    end
  end

  private

  #Setting default values - before_create is called and sets the following
  def default_values
    self.state ||= "requested"
    self.color ||= "#000000"
    self.type_id||= 1
  end
end

似乎不明白为什么这在开发和生产中都有效,而不是在 heroku 中。有什么想法吗?

【问题讨论】:

  • 您可能只需要使用heroku run rake db:migrate 迁移您的数据库。之后不要忘记使用 heroku restart 重新启动您的应用,以便它接收这些更改。

标签: ruby-on-rails ruby-on-rails-3 postgresql heroku rails-activerecord


【解决方案1】:

标准 SQL 字符串使用单引号,双引号用于标识符(例如表名和列名); PostgreSQL 遵循这里的标准,MySQL 和 SQLite 不那么严格,其他数据库执行其他操作的严格程度不同。在任何情况下,SQL 字符串文字的单引号在任何地方都应该是一样的。

您在 SQL 字符串上使用双引号:

@holidays = Holiday.find(:all, :conditions => 'state = "requested"')
#------------------------------------------------------^---------^

你必须用单引号:

@holidays = Holiday.find(:all, :conditions => %q{state = 'requested'})

或者对其进行现代化改造,让 ActiveRecord 处理引用:

@holidays = Holiday.where(:state => 'requested')

您可能还需要修复此引用:

@holidays = Holiday.find(:all, :conditions => ["approver_id = #{current_user.id}", "state = requested"])

同样,现代化是最简单的方法:

@holidays = Holiday.where(:approver_id => current_user.id, :state => 'requested')

我猜你是在 SQLite 上开发,但在 PostgreSQL 上部署。这是个坏主意,总是在同一个堆栈上开发和部署。

【讨论】:

  • 我刚刚发布此消息后意识到:P。每天学习一些东西。我正在 MySQL 上开发
  • 现代化的方式看起来更好更干净的方式
  • @user532339:它也是可链接的(M.where(...).where(...).order(...)),相当方便。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2022-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 2012-12-16
  • 1970-01-01
相关资源
最近更新 更多