Question

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?

Was it helpful?

Solution

Check the data type of the columns and your time zone. You may be confusing timestamp with time zone and timestamp.

Looks like you have timestamp in your table, but query with timestamptz. This way, it all depends on the local time zone of your session (which defaults to the time zone of the server if not specified otherwise.)

Switch both to timestamptz, or timestamp if time zones are completely irrelevant to you. (If in doubt, use timestamptz.)

Not the cause of your problem, but your query should probably be:

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 is almost always wrong for timestamp types due to fractional numbers! What would your query do with starttime = '2013-03-21T23:59:59.123+00'?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top