0

I have created a little test app to track down a problem I experienced with Postgres on Heroku: http://snippi.com/s/xd511rf

As you can see in line 49, I want to retrieve all entries created today. This would be the first two items of my test data with the Ruby Gem DataMapper.

When I run this app on my notebook (Ubuntu 12.10, HP, Ruby 1.9.3) everything I get this result, which is right:

[
{
    "id": 1,
    "text": "Working on some awsomenewss",
    "category": 0,
    "starttime": "2013-03-21T15:56:00+01:00",
    "endtime": "2013-03-21T18:26:00+01:00",
    "creation": "2013-03-21T16:15:21+01:00"
},
{
    "id": 2,
    "text": "facebooking",
    "category": 0,
    "starttime": "2013-03-21T20:48:00+01:00",
    "endtime": "2013-03-21T22:26:00+01:00",
    "creation": "2013-03-21T16:15:21+01:00"
}
]

In my debug console this SQL query is logged:

SELECT "id", "text", "category", "starttime", "endtime", "creation" 
  FROM "entries" 
  WHERE "starttime" 
    BETWEEN '2013-03-21T00:00:00+00:00' 
      AND '2013-03-21T23:59:59+00:00' 
  ORDER BY "id"

But after pushing the app to Heroku a very strange error occurrs. When I run it now (http://afternoon-everglades-4239.herokuapp.com/) this is the response:

[]

Why is it empty?

The data is definitely in the database which is proved by this Dataclip from Heroku: https://dataclips.heroku.com/hygziosyxwperyctwfbhjzgbzhbj

Also when I run the SQL command manually via ´heroku pg:psql´ it actually works with this output:

 id |            text             | category |      starttime      |       endtime       |      creation       
----+-----------------------------+----------+---------------------+---------------------+---------------------
  1 | Working on some awsomenewss |        0 | 2013-03-21 15:56:00 | 2013-03-21 18:26:00 | 2013-03-21 16:15:21
  2 | facebooking                 |        0 | 2013-03-21 20:48:00 | 2013-03-21 22:26:00 | 2013-03-21 16:15:21
(2 rows)

The logs do not contain any errors or further information. I have used a Remote Heroku PostgreSQL Database in both cases (Production and Local).

So why does this not work?

4

1 回答 1

3

检查列的数据类型和您的时区。您可能会感到困惑timestamp with time zone并且timestamp.

看起来timestamp您的表中有,但使用timestamptz. 这样,这一切都取决于会话的本地时区(如果未另行指定,则默认为服务器的时区。)

切换到timestamptz,或者timestamp如果时区与您完全无关。(如有疑问,请使用timestamptz。)

不是您的问题的原因,但您的查询可能应该是:

SELECT id, text, category, starttime, endtime, creation 
FROM   entries 
WHERE  starttime >= timestamp '2013-03-21' -- defaults to 00:00 time
AND    starttime <  timestamp '2013-03-22'
ORDER  BY id

a BETWEEN x AND y由于小数,类型几乎总是错误的!timestamp您的查询会做starttime = '2013-03-21T23:59:59.123+00'什么?

于 2013-03-21T16:41:52.693 回答