NOW()无法在查询中使用(它不存在)

huangapple go评论98阅读模式
英文:

NOW() can't be used in query (it doesn't exist)

问题

在我的Go代码中,我有这行代码:

rows, err := conn.Query(`
	SELECT id, name, lang, deleted_at, read
	FROM categories
	WHERE deleted_at < NOW()
`)

当我运行这段代码时,我从PostgreSQL得到一个错误:

pq: function now() does not exist

(pq是我使用的Go驱动程序)

使用CURRENT_TIMESTAMP也会引发错误。它说列current_timestamp不存在。在psql(命令行)中直接使用NOW()不会引发该错误,一切正常运行。

对于为什么我无法在我的Go应用程序中发送查询时使用NOW()CURRENT_TIMESTAMP,有什么想法吗?

英文:

In my Go code, I have this line of code:

rows, err := conn.Query(`
	SELECT id, name, lang, deleted_at, read
	FROM categories
	WHERE deleted_at &lt;&#160;NOW()
`)

When I run this, I get an error from PostgreSQL:

pq: function &#160;now() does not exist

(pq is the Go driver that I use)

Using CURRENT_TIMESTAMP also provokes an error. It says that the column current_timestamp does not exist. Using NOW() directly in psql (command line) does not provoke that error and everything works fine.

Any idea on why I can't use NOW() nor CURRENT_TIMESTAMP when sending a query from my Go app?

答案1

得分: 1

我的键盘上的右Alt键卡住了,我没有注意到。这导致我在按下空格键时插入了不可断空格。PostgreSQL可能将不可断空格解释为函数名称的一部分。

改为插入普通空格解决了这个问题。

英文:

My right alt key on my keyboard got stuck without me noticing. That made me insert non-breakable spaces when pressing the space bar. PostgreSQL probably interpreted the non-breakable space as a character that is part of the function's name.

Inserting regular spaces instead solved the issue.

答案2

得分: 0

根据我所看到的,你的查询没有问题。当实际存在该函数但期望的值类型不同时,PostgreSQL也可能会抛出函数未找到的错误。

函数now()返回的是带有时区的时间戳。如果deleted_at列的类型与其不同,你可能需要将deleted_at或now()的输出进行类型转换。

英文:

As far as I can see there is nothing wrong with your query. PostgreSQL can throw function not found at you also when the function actually exists but a different value type is expected.

The function now() returns timestamp with time zone. If the column deleted_at is not of the same type, you might need to cast either deleted_at or the output from now().

huangapple
  • 本文由 发表于 2015年11月2日 04:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/33466952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定