Ruby on Rails:SQLite3到PostgreSQL的null日期

时间:2012-09-12 10:23:13

标签: ruby-on-rails ruby sqlite postgresql date

我正在开发中使用SQLite3数据库,现在已经开始使用heroku来托管我的应用程序。 Heroku只支持我在GEM文件中修改的PostgreSQL

group :development, :test do
 gem 'sqlite3'
end
group :production do
  gem 'pg'
end

现在,一切正常,除非我查询日期,我收到此错误

    ActionView::Template::Error (PG::Error: ERROR:  invalid input syntax for type date: ""
2012-09-12T10:03:03+00:00 app[web.1]: 
2012-09-12T10:03:03+00:00 app[web.1]: LINE 1: ...-12' AND '2012-09-12' OR DATE(start_date) BETWEEN '' AND '')...
2012-09-12T10:03:03+00:00 app[web.1]:                                                              ^
2012-09-12T10:03:03+00:00 app[web.1]:     137: <% if @project_search.total_entries > 0 %>
2012-09-12T10:03:03+00:00 app[web.1]:     136: 
2012-09-12T10:03:03+00:00 app[web.1]: : SELECT COUNT(*) FROM "projects"  WHERE (client LIKE '%%' AND industry LIKE '%%' AND role LIKE '%%' AND tech LIKE '%%' AND business_div LIKE '%%' AND project_owner LIKE '%%' AND exception_pm LIKE '%%' AND status LIKE '%%' AND (DATE(start_date) BETWEEN '2011-09-12' AND '2012-09-12' OR DATE(start_date) BETWEEN '' AND '') AND keywords LIKE '%%')):
2012-09-12T10:03:03+00:00 app[web.1]:     138: <% if @search_performed %>
2012-09-12T10:03:03+00:00 app[web.1]:     140: <style>
2012-09-12T10:03:03+00:00 app[web.1]:     139:

这是我的project.rb,因为我认为这是我需要改变的地方:

class Project < ActiveRecord::Base
  attr_accessible :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech

#validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech



def self.search(search_client, search_industry, search_role, search_tech, search_business_div, search_project_owner, search_exception_pm, search_status, search_start_date_dd, search_start_date_A, search_start_date_B,  search_keywords) 
  return scoped unless search_client.present? || search_industry.present? || search_role.present? || search_tech.present? || search_business_div.present? || search_project_owner.present? || search_exception_pm.present? || search_status.present? || search_start_date_dd.present?  || search_start_date_A.present?  || search_start_date_B.present?  || search_keywords.present?


todays_date = DateTime.now.to_date

if !search_start_date_A.blank? or !search_start_date_B.blank?
    search_start_date_A = Date.parse(search_start_date_A).strftime("%Y-%m-%d")
    search_start_date_B = Date.parse(search_start_date_B).strftime("%Y-%m-%d")
    todays_date = ""
    end

if search_start_date_dd.blank?
    todays_date = ""
end


if (search_start_date_A.blank? or search_start_date_B.blank?) and search_start_date_dd.blank?

where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND keywords LIKE ?', 
      "%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" , 
      "%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%", 
       "%#{search_keywords}%"
    ])

else

where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND (DATE(start_date) BETWEEN ? AND ? OR DATE(start_date) BETWEEN ? AND ?) AND keywords LIKE ?', 
      "%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" , 
      "%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%", 
      search_start_date_dd, todays_date , search_start_date_A, search_start_date_B, "%#{search_keywords}%"
    ])
end
end


def self.paginated_for_index(projects_per_page, current_page)
    paginate(:per_page => projects_per_page, :page => current_page)
  end

end

似乎SQLite3可以处理&#39;&#39;作为一个空日期,但PG不能

有没有人对此有解决方案?

我是rails的新手,所以在回答时请记住这一点。提前谢谢。

1 个答案:

答案 0 :(得分:3)

NULL''(空字符串)是不同的东西。一些数据库,比如MySQL和显然是SQLite,允许你将它们视为同一个东西,但它们不是。 ''不是有效日期。 PostgreSQL也不会接受另一个常见的MySQL-ism,0000-00-00;如果您习惯将其用作占位符或“无效”值,请注意这一点。

您需要修复应用程序,以便它为空日期发送NULL。我希望将日期设置为应用程序级别的null / nil值(Ruby似乎使用nil作为其空值)而不是""将成功。