如何在 PGSQL 中按日期搜索?

发布于 2024-10-30 13:47:40 字数 933 浏览 2 评论 0原文

我收到此错误:

ActionView::Template::Error (PGError: ERROR: 操作符不存在: 没有时区的时间戳 ~~ 未知 LINE 1: ... "articles" WHERE ("articles"."created_at" LIKE ' 2010...

我有一个存档控制器,可以按流派和年、月、日动态显示文章,无论这些字段在 mysqlite 中可用,在 mysqlite 中,我有这个索引操作:

def index
  filter_title
  @articles = Article.where(:created_at.matches % date_builder, :genre.matches % genre_builder).order("created_at DESC")
  respond_to do |format|
    format.json { render :json => @articles }
    format.xml  { render :xml => @articles }
    format.html
  end
end

以及这个 date_builder 函数

def date_builder
  @date = ""
  @date += params[:year] if !(params[:year].nil?)
  @date += "-" + params[:month] if !(params[:month].nil?)
  @date += "-" + params[:day] if !(params[:day].nil?)
  @date += "%"
  @date
end

。使用metawhere 查找与我提供的字符串部分相匹配的日期。这在Sqlite 中完美运行。我已将我的应用程序迁移到heroku 和PGSQL,但它不允许我执行此操作,谢谢。 !

I am getting this error:

ActionView::Template::Error (PGError: ERROR: operator does not exist: timestamp without time zone ~~ unknown LINE 1: ... "articles" WHERE ("articles"."created_at" LIKE '2010...

I have an archive controller where I can dynamically display articles by genre and year, month and day, with whichever of those fields are available in the url. In mysqlite, I had this index action:

def index
  filter_title
  @articles = Article.where(:created_at.matches % date_builder, :genre.matches % genre_builder).order("created_at DESC")
  respond_to do |format|
    format.json { render :json => @articles }
    format.xml  { render :xml => @articles }
    format.html
  end
end

And this date_builder function

def date_builder
  @date = ""
  @date += params[:year] if !(params[:year].nil?)
  @date += "-" + params[:month] if !(params[:month].nil?)
  @date += "-" + params[:day] if !(params[:day].nil?)
  @date += "%"
  @date
end

that would use metawhere to find dates that matched the part of the string that I supplied. This worked perfectly in Sqlite. I have migrated my application onto heroku and PGSQL, and it doesn't allow me to do this. What is the solution? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-11-06 13:47:40

在 postgres 中,时间戳不像 sqllite 那样存储为字符串:

select 'A' where localtimestamp like '2011-04-04%';
ERROR:  operator does not exist: timestamp without time zone ~~ unknown

使用 >= 代替怎么样?

select 'A' where localtimestamp >= '2011-04-04';

 ?column?
----------
 A
(1 row)

In postgres, a timestamp is not stored as a string like in sqllite:

select 'A' where localtimestamp like '2011-04-04%';
ERROR:  operator does not exist: timestamp without time zone ~~ unknown

how about using >= instead?

select 'A' where localtimestamp >= '2011-04-04';

 ?column?
----------
 A
(1 row)
虫児飞 2024-11-06 13:47:40

虽然我喜欢@JackPDouglas的答案,但我想尝试尽可能地将sql代码保留在我的项目之外,所以我最终这样做了:

...
date_builder
  if !(params[:year].nil?)
    @articles = Article.where(
      {:created_at.gt => @datelower} &
      {:created_at.lt => (@datehigher - 1.second)} &
      :genre.matches % genre_builder
      ).order("created_at DESC")
  else
...

以及日期的这个方法:

def date_builder
  if !(params[:day].nil?)
    @datelower = Time.utc(params[:year], params[:month], params[:day])
    @datehigher = @datelower + 1.day
  elsif !(params[:month].nil?)
    @datelower = Time.utc(params[:year], params[:month], 01)
    @datehigher = @datelower + 1.month
  elsif !(params[:year].nil?)
    @datelower = Time.utc(params[:year], 01, 01)
    @datehigher = @datelower + 1.year
  end
end

While I liked @JackPDouglas answer, I wanted to try and keep sql code out of my project as much as possible, so i ended up doing this:

...
date_builder
  if !(params[:year].nil?)
    @articles = Article.where(
      {:created_at.gt => @datelower} &
      {:created_at.lt => (@datehigher - 1.second)} &
      :genre.matches % genre_builder
      ).order("created_at DESC")
  else
...

and this method for the date:

def date_builder
  if !(params[:day].nil?)
    @datelower = Time.utc(params[:year], params[:month], params[:day])
    @datehigher = @datelower + 1.day
  elsif !(params[:month].nil?)
    @datelower = Time.utc(params[:year], params[:month], 01)
    @datehigher = @datelower + 1.month
  elsif !(params[:year].nil?)
    @datelower = Time.utc(params[:year], 01, 01)
    @datehigher = @datelower + 1.year
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文