为什么轨道/ ActiveRecord的给出一个Postgres的SyntaxError"为了&QUOT非整型常量;?

问题描述:

我对Rails的一些红宝石/ ActiveRecord的code,它是给我下面的Postgres的错误:

I have some Ruby on Rails / ActiveRecord code that is giving me the following Postgres error:

PG ::语法错误:错误:为了非整型常量

PG::SyntaxError: ERROR: non-integer constant in ORDER

我不知道为什么,因为它是一个简单的访存created_at下令

I'm not sure why since it is a simple fetch ordered by created_at

self.posts.order(created_at: :desc).limit(25)

我该怎么做才能改变呢?

What do I do to change this?

我交换我的Rails应用程序从MySQL到PostgreSQL(我的发展环境和创业板上市是在这个岗位的底部)之后,已经经历过这样的错误,以及

I have experienced this error as well after switching my Rails app from MySQL to PostgreSQL (my development environment and Gem list are at the bottom of this post).

该错误似乎是由引起的PostgreSQL在SQL查询中预期的列名被双引号的,因为我可以改变我的.order()参数的散列格式的文字,以消除误差字符串:

The error appears to be caused by PostgreSQL expecting the column names in a SQL query to be double-quoted, as I am able to eliminate the error by changing my ".order()" parameter from hash-format to a literal string:

这Rails的code触发错误:

This Rails code triggers the error:

ModelName.where(:attribute => self.id).order(col1: :desc, col2: :asc)

...和所产生的误差:

...and the resulting error:

Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
ModelName Load (1.0ms)  SELECT "model_name".* FROM "model_name" WHERE ("model_name"."attribute" = 14) ORDER BY '{:col1=>:desc, :col2=>:asc}'
PG::SyntaxError: ERROR:  non-integer constant in ORDER BY
LINE 1: ...E ("model_name"."attribute" = 14) ORDER BY '{:col1=...
                                                      ^

而这个滑轨code ++工程,而不会触发错误消息:

Whereas this Rails code works without triggering the error message:

ModelName.where(:attribute => self.id).order("\"col1\" desc, \"col2\" asc")

我知道,Pos​​tgreSQL是能够正确地跨preT不带引号的标识符,但是ActiveRecord的在这种情况下产生的查询的格式似乎是暧昧到PostgreSQL。下面是PostgreSQL的查询语法的说明: HTTP://www.postgresql .ORG /文档/ 9.1 /静电/ SQL的语法lexical.html

下面是我的开发环境:

  • 在Windows 7中
  • 红宝石2.1.5p273
  • 的Rails 3.0.3(我知道这是老......)
  • 的PostgreSQL 9.4(在Windows上)

......这是我的创业板上市的相关部分:

...and here's the pertinent part of my gem list:

*** LOCAL GEMS ***

abstract (1.0.0)
afm (0.2.2)
arel (2.0.10)
builder (2.1.2)
bundler (1.7.7)
hashery (2.1.1)
i18n (0.6.11)
mysql (2.9.1)
pg (0.18.1 x86-mingw32)
rack (1.2.8)
rails (3.0.3)
railties (3.0.3)
rake (0.9.2.2)
ruby-rc4 (0.1.5)
sequel (3.31.0)
valkyrie (0.0.2)