Rails PG::Error 这个问题困扰我好久,求解释

chunlea · 2012年09月01日 · 最后由 ywencn 回复于 2012年11月24日 · 3159 次阅读


Started GET "/admin/buildings" for at 2012-09-01 09:24:47 +0000
Processing by Admin::BuildingsController#index as HTML
Completed 500 Internal Server Error in 1ms

ActiveRecord::StatementInvalid (PG::Error: ERROR:  prepared statement "a1" already exists
:             SELECT COUNT(*)
            FROM pg_class c
            LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind in ('v','r')
            AND c.relname = $1
            AND n.nspname = ANY (current_schemas(false))
  app/controllers/admin/buildings_controller.rb:5:in `index'


初步判断时数据库的问题,可是好像没有解决方案啊。真是囧啊。 目前先用 SQLite3 在服务器上顶一会。 SQLite 没有这种情况

pg 好像对某些 sql 语句不支持,以前遇到过 pg 不行但 sqlite 行的。不知是不是这个原因

#2 楼 @pongyo 里面提到的方案正解!我稍后整理一下。终于解决了。

WU Jun 5 months ago
A simple note:
pg gem uses prepare statement in production env by default, which is different from MySQL.
This can be an issue if you are using unicorn. You may got PGError: ERROR: prepared statement "a3" already exists after several requests.
To solve this, make sure each unicorn process uses its own database connection by adding this to unicorn config file:
after_fork do |server, worker|
Hope this helps :)


需要 登录 后方可回复, 如果你还没有账号请点击这里 注册