【发布时间】:2011-04-05 19:00:59
【问题描述】:
我最近一直在向 Heroku 部署一些应用程序。我在本地开发机器上运行 MySQL,并花了一些时间更新我的一些范围以在 PostgreSQL 中工作。但是,我收到的错误证明很难更改。
目前我的模型中有一个特定于数据库的案例语句。我理解为什么会发生有关 MySQL 日期函数的错误,但我不确定这是否是最有效的解决方案。有没有人有更好的方法来实现适用于 MySQL 和 PostgreSQL 的修复?
case ActiveRecord::Base.connection.adapter_name
when 'PostgreSQL'
named_scope :by_year, lambda { |*args| {:conditions => ["published = ? AND (date_part('year', created_at) = ?)", true, (args.first)], :order => "created_at DESC"} }
named_scope :by_month, lambda { |*args| {:conditions => ["published = ? AND (date_part('month', created_at) = ?)", true, (args.first)], :order => "created_at DESC"} }
named_scope :by_day, lambda { |*args| {:conditions => ["published = ? AND (date_part('day', created_at) = ?)", true, (args.first)], :order => "created_at DESC"} }
else
named_scope :by_year, lambda { |*args| {:conditions => ["published = ? AND (YEAR(created_at) = ?)", true, (args.first)], :order => "created_at DESC"} }
named_scope :by_month, lambda { |*args| {:conditions => ["published = ? AND (MONTH(created_at) = ?)", true, (args.first)], :order => "created_at DESC"} }
named_scope :by_day, lambda { |*args| {:conditions => ["published = ? AND (DAY(created_at) = ?)", true, (args.first)], :order => "created_at DESC"} }
end
仅供参考,这是我得到的 PostgreSQL 错误:
PGError: ERROR: function month(timestamp without time zone) does not exist LINE 1: ...T * FROM "articles" WHERE (((published = 't' AND (MONTH(crea... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. : SELECT * FROM "articles" WHERE (((published = 't' AND (MONTH(created_at) = '11')) AND (published = 't' AND (YEAR(created_at) = '2010'))) AND ("articles"."published" = 't')) ORDER BY created_at DESC LIMIT 5 OFFSET 0
提前感谢任何人的任何意见。
【问题讨论】:
标签: mysql ruby-on-rails database postgresql heroku