6

这有什么问题吗(用户模型,第 95 行):

scope :by_recent, where('created_at BETWEEN ? AND ?', 1.month.ago, Time.now).order('created_at desc').

每当我启动 rails s 或 rails c 时,我都会得到:

 /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log': PGError: ERROR:  unrecognized time zone name: "UTC" (ActiveRecord::StatementInvalid)

在我的模型中评论该行会使错误消失。奇怪的。顺便说一句,我正在使用 postgres 9。通过 Homebrew (brew install posgtres) 安装。

链接建议以下内容。但我不明白这个人建议的解决方案是什么......

MikZ:我想通了

当您覆盖访问器(使用表格列)时,rails 正在设置具有自动时区转换的访问器,您将遇到严重的问题

当离开rails默认(或设置时区)时,你会得到很好的结果,但如果你覆盖访问器,你会遇到严重的问题

所以:永远不要覆盖“时间”属性的访问器

完整错误:

/Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log': PGError: ERROR:  unrecognized time zone name: "UTC" (ActiveRecord::StatementInvalid)
: SET time zone 'UTC'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:194:in `log'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:496:in `execute'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:961:in `configure_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:939:in `connect'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:228:in `initialize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `new'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/postgresql_adapter.rb:25:in `postgresql_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in `new_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `checkout_new_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `block (2 levels) in checkout'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `loop'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in `block in checkout'
    from /Users/Chris/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `checkout'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:93:in `connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in `retrieve_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in `retrieve_connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:1330:in `replace_bind_variables'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:1317:in `sanitize_sql_array'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:1224:in `sanitize_sql_for_conditions'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/relation/query_methods.rb:206:in `build_where'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/relation/query_methods.rb:77:in `where'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/base.rb:441:in `where'
    from /Users/Chris/Sites/site_name/app/models/user.rb:95:in `<class:User>'
    from /Users/Chris/Sites/site_name/app/models/user.rb:1:in `<top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:454:in `block in load_file'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:453:in `load_file'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:340:in `require_or_load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:491:in `load_missing_constant'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:183:in `block in const_missing'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:181:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:181:in `const_missing'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:124:in `block in constantize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:123:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/inflector/methods.rb:123:in `constantize'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:528:in `block in <class:Reference>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:538:in `yield'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:538:in `default'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:538:in `get'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/mapping.rb:76:in `to'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/rails/routes.rb:164:in `block in devise_for'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/rails/routes.rb:160:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/bundler/gems/devise-bbaaefa995c3/lib/devise/rails/routes.rb:160:in `devise_for'
    from /Users/Chris/Sites/site_name/config/routes.rb:75:in `block in <top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:233:in `instance_exec'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/routing/route_set.rb:233:in `draw'
    from /Users/Chris/Sites/site_name/config/routes.rb:1:in `<top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `block in load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:235:in `load'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:127:in `block in reload_routes!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:127:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:127:in `reload_routes!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:120:in `block in routes_reloader'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/file_update_checker.rb:32:in `call'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:51:in `block (2 levels) in <module:Finisher>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:52:in `call'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:52:in `block in <module:Finisher>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `block in run_initializers'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
    from /Users/Chris/Sites/site_name/config/environment.rb:5:in `<top (required)>'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:103:in `require_environment!'
    from /Users/Chris/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:22:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

编辑:

实际上,问题与这个问题有关。我也从 macports 搬到了自制软件。在使用自制软件安装 postgres 之前,我需要删除 /opt/local 吗?

其次,我可以执行 rake db:drop:all 和 rake db:create,但不能执行 rake db:migrate。我得到:

rake aborted!
PGError: ERROR:  unrecognized time zone name: "UTC"
: SET time zone 'UTC'
4

5 回答 5

13

是的,似乎数据库出现了严重问题。我记得通过自制软件安装 PostgreSql,但可能没有停止 postgres 服务器的 macport 安装,它仍在后台运行。

在另一位开发人员(RhodiumToad)的帮助下,我通过以下步骤解决了这个问题:

  1. 停止服务器
  2. 重新运行initdb
  3. 再次启动服务器

早些时候,我通过 rm -rf /usr/local/var/postgres 删除了 /usr/local/var/postgres。所以这就是我需要做的:

ps axw

找到 postgres 服务器

117   ??  S      0:00.23 /usr/local/Cellar/postgresql/9.0.3/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.

杀死它,这也应该杀死在 bg 中运行的任何其他 postgres 进程

kill 117

以上杀死了进程并停止了服务器。现在我不得不初始化数据库,一切似乎又可以正常工作了:

initdb /usr/local/var/postgres

希望这可以帮助遇到同样问题的人。

于 2011-02-20T11:56:55.197 回答
5

我的问题是我运行了多个 PostgreSQL 服务器实例。杀死每个进程都有效并仅重新启动我想要的进程。不知道这是为什么PGError: ERROR: unrecognized time zone name: "UTC" : SET time zone 'UTC'

 ps axw | grep postgres

然后kill就像克里斯蒂安上面所说的那样。

注意:我不需要再次运行 initdb。

于 2012-08-10T13:46:58.967 回答
0

不是 100% 确定,但是您定义范围的方式会从您传入的时间参数中生成一个字符串,这可能会导致一些问题。尝试使用符号,看看是否有效:

scope { :conditions => {:created_at => 1.month.ago..Time.now}, :order => 'model.created_at DESC' }
于 2011-02-19T16:58:15.820 回答
0

好吧,我猜你模型中的那一行中的某些东西使它在与数据库交谈时尝试修复正在使用的时区。我想这有点道理...

无论如何,假设字符串SET time zone 'UTC'是在连接时发送到 postgresql 的命令(看起来就是这种情况,查看那个回溯),那么我希望它能够工作。当然,它适用于我自己(Linux)安装的 postgresql。如果您使用 psql 连接到数据库并手动发出命令,您能否验证它是否失败?

它似乎确实表明 postgresql 安装损坏。清理东西并重新安装将是一个合理的尝试步骤。我自己安装的 postgresql 正在使用来自操作系统的时区数据,即在/usr/share/zoneinfo. 您的安装可能会尝试从某个位置获取数据,该位置在构建期间存在,但在您转换后已被删除。我相信应该是它在配置时找不到带有数据的位置,它会安装自己的捆绑副本。

于 2011-02-19T17:04:08.683 回答
0

发生此错误是因为 Postgres 已升级但未重新启动。运行的旧版本正在寻找不存在的时区文件。

就像这里所说的那样重新启动:https ://stackoverflow.com/a/20566779/1682263

于 2014-04-14T08:16:00.360 回答