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

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

部署后老是出错,错误信息如下:

Started GET "/admin/buildings" for 112.234.125.127 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|
  ActiveRecord::Base.establish_connection
end
===================================
Hope this helps :)

靠。。我今天也遇到了。。

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