function UNIX_TIMESTAMP does not exist

断了今生、忘了曾经 提交于 2021-02-17 06:12:14

问题


I am trying to convert my datetime to unix timestamp. I've tried doing several ways and the error is all the same. I am not very sure what I am suppose to do.

date_joined is like this 2017-09-30 10:24:44.954981+00:00

function unix_timestamp(timestamp with time zone) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.


User.objects.annotate(photo_time=Func(F('date_joined'),function='UNIX_TIMESTAMP'))
User.objects.extra(select={'photo_time':"to_unixtime(date_joined)"})
#also tried and UNIX_TIMESTAMP

回答1:


That's because Postgres won't let you do that (see here). If you really don't need the UNIX_TIMESTAMP, you need Extract

User.objects.annotate(photo_time=Extract('date_joined', 'epoch').get())

Of course, you could also define a TO_UNIXTIME stored procedure/function, but that seems a bit over the top.



来源:https://stackoverflow.com/questions/47445704/function-unix-timestamp-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!