Sqlite到Postgres(Heroku)日期/时间 [英] Sqlite to Postgres (Heroku) Date/Time

查看:148
本文介绍了Sqlite到Postgres(Heroku)日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Heroku(postgres)中推送Rails 3应用程序到生产中时出现错误,而本地代码(sqlite)代码正常运行时 - 我无意中看到Postgres手册:

I am getting an error when pushing a Rails 3 app into production on Heroku (postgres) while locally (sqlite) the code works fine - I took at look at the Postgres manual to no avail:

我的代码:


(strftime('%s','now ') - strftime('%s',
posts.created_at)

(strftime('%s','now') - strftime('%s', posts.created_at)

错误:

Error:


第1行:SELECT posts。*,
((strftime('%s','now') - .. 。提示:没有
函数匹配给定的名称和
参数类型。您可能需要添加
显式类型转换。

LINE 1: SELECT posts.*, ((strftime('%s','now') -...
HINT: No function matches the given name and argument types. You might need to add explicit type casts.


推荐答案

在PostgreSQL中,您没有函数strftime。要计算日期之间的秒数,您可以使用:

In PostgreSQL you don't have a function strftime. To compute the number of seconds between to dates you can use:

SELECT extract(epoch from now() - posts.created_at);

在PostgreSQL中说你有t也很重要他是间隔数据类型的,所以如果你只是减去(now() - posts.created_at),它会为你返回一个很好的间隔类型,这很容易用to_char格式化。

It's also important to remark that in PostgreSQL you have the interval data type, so if you just subtract (now() - posts.created_at) it will return a nice interval type for you, which can be easily formated with to_char.

这篇关于Sqlite到Postgres(Heroku)日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆