Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator does not exist: interval > integer

Tags:

sql

postgresql

I have a query that works on Postgresql 7.4 but not on Postgresql 8.3 with same database.

Query:

SELECT * FROM login_session WHERE (now()-modified) > timeout;

Gets the following error:

ERROR:  operator does not exist: interval > integer
LINE 1: ...ELECT * FROM login_session WHERE (now()-modified) > timeout ...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Column modified is a timestamp and timeout is an integer.

Is there some settings I need to change on server?

I'm installing an application for a client on new server (ubuntu) so I can't change queries in the application.

like image 641
walming Avatar asked Nov 14 '08 14:11

walming


2 Answers

There are a lot of changes between 7.4 and 8.3. Some of the most drastic were the removal of some automatic casts.

I suppose the "timeout" is in seconds? If so you could change the query to:

SELECT
    *
FROM
    login_session
WHERE
    (CURRENT_TIMESTAMP - modified) > (timeout * '1 sec'::interval);
like image 131
Milen A. Radev Avatar answered Sep 22 '22 00:09

Milen A. Radev


create or replace function int2interval (x integer) returns interval as $$ select $1*'1 sec'::interval $$ language sql;
create cast (integer as interval) with function int2interval (integer) as implicit;

ought to do it.

like image 41
geocar Avatar answered Sep 22 '22 00:09

geocar