简体   繁体   English

Ruby on Rails:仅当托管在heroku上时,Active Record查询才能执行。

[英]Ruby on Rails: Active Record query fails to execute only when hosted on heroku.

I have a page which simply displays all of the Links in my database sorted by the voteCount. 我有一个页面,该页面仅显示数据库中按“投票计数”排序的所有链接。 Here is the controller: 这是控制器:

class PagesController < ApplicationController
  def index
    params[:per_page] ||= 5
    params[:page]     ||= 1

    @links = Link.order('voteCount DESC').paginate(:page => params[:page], :per_page => params[:per_page])

  end
end

I save query the database using the paginate plugin, and prepend it with: 我使用paginate插件保存查询数据库,并在其前面添加:

.order('voteCount DESC')

When I run this command on my local server, it runs fine. 当我在本地服务器上运行此命令时,它运行正常。 However, as soon as I deploy it to heroku, it fails. 但是,一旦我将其部署到heroku,它就会失败。 This is the output I get when I check the logs/execute it in the console: 这是我在控制台中检查日志/执行日志时得到的输出:

Link Load (2.0ms)  SELECT "links".* FROM "links" ORDER BY voteCount DESC LIMIT 5 OFFSET 0
ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "votecount" does not exist
LINE 1: SELECT  "links".* FROM "links"  ORDER BY voteCount DESC LIMI...
                                                 ^

I've checked using the console, the voteCount column is definitely there. 我已经使用控制台检查过,voteCount列肯定在那里。 This might be due to the fact that my local environment runs sqlite3 and heroku makes me use postgres .... 这可能是由于我的本地环境运行sqlite3,而heroku使我使用了postgres ....

Any help would be really appreciated. 任何帮助将非常感激。 Thanks. 谢谢。

You have a case sensitivity problem. 您有区分大小写的问题。 When you say something like: 当您说类似:

create_table :links do |t|
  t.integer :voteCount
end

Rails will send SQL like this to PostgreSQL to create the column: Rails会将这样的SQL发送到PostgreSQL来创建列:

"voteCount" integer

The double quotes around the identifier make it case sensitive and that means that you have to refer to it as "voteCount" forevermore. 标识符周围的双引号使它区分大小写,这意味着您必须永远将其称为"voteCount" If you say this: 如果您这样说:

.order('"voteCount" DESC')

everything will should work. 一切都会正常。

SQLite doesn't care about identifier case so you could say voteCount , votecount , VOTECOUNT , ... in SQLite and it wouldn't matter. SQLite不在乎标识符的大小写,因此您可以在SQLite中说voteCountvotecountVOTECOUNT ,...,这没关系。

Rails always quotes the identifiers that it produces when talking to PostgreSQL. 与PostgreSQL通讯时,Rails总是引用其产生的标识符。 Normally this doesn't matter, the Rails convention is to use lower case identifiers and PostgreSQL folds unquoted identifiers to lower case before using them so everything works by default. 通常这没关系,Rails约定是使用小写标识符,PostgreSQL在使用前将未引用的标识符折叠成小写,因此默认情况下一切正常。

There are some relevant best practices here: 这里有一些相关的最佳做法:

  1. Develop and deploy on the same stack. 在同一堆栈上进行开发和部署。
  2. Use lower case column names with words separated by underscores with PostgreSQL. 在PostgreSQL中,使用小写的列名,并用下划线将单词分隔。 This also happens to match the usual Ruby conventions. 这也恰好符合通常的Ruby约定。

I'd recommend that you use vote_count as the column name instead. 我建议您改为使用vote_count作为列名。

您的heroku应用程序可能尚未迁移该列

heroku run rake db:migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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