PGError:错误:运算符不存在:布尔值 ~~* 未知
在本地,我使用 SQLite 进行开发,但在生产主机上我运行 PostgreSQL。在本地一切都很好,但在生产主机上却并非如此。
我已经构建了一种搜索表单,通过它我可以使用我想要的任何组合来评估数据库中的所有数据。只要我不使用布尔值和/或日期字段,这似乎就可以正常工作。 PostgreSQL 似乎不太喜欢我的代码...
所以,这里有一些示例代码:
unless params[:analysis][:sporty].blank?
tmp_conditions_customer << ["(sporty ILIKE ?)", "%#{params[:analysis][:sporty]}%"]
end
其计算结果为
SELECT COUNT(*) FROM "customers" WHERE ((sporty ILIKE '%%')
“为什么会这样?”为什么是“%%”?
为了测试部署,我使用 Heroku 和 Exceptional 插件。该插件为我提供了以下提示:
提示:没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。
谢谢 Exceptional,但这到底意味着什么? :-D SQL 查询的类型转换?怎么样?
在我的迁移中,数据库字段如下所示:
t.boolean :sporty
在我创建此数据的表单中,我使用此代码
<%= f.label :sporty %><br />
<%= f.select :sporty, options_for_select({ "Ja" => true, "Nein" => false }), { :include_blank => '-----'} %>
正如我已经提到的,SQLite 是我的朋友,似乎是对 PostgreSQL 更严格的评估,这导致了麻烦。
提前感谢您的帮助。
Locally I’m developing with SQLite, but on my production host I’m running PostgreSQL. Locally everything’s fine but not so on the production host.
I have built kind of a search form with which I can evaluate all data in my database with any combination I’d like. This seems to work fine as long as I don’t use boolean and/or date fields. PostgreSQL doesn’t seem to like my code very much…
So, here’s some example code:
unless params[:analysis][:sporty].blank?
tmp_conditions_customer << ["(sporty ILIKE ?)", "%#{params[:analysis][:sporty]}%"]
end
This evaluates to
SELECT COUNT(*) FROM "customers" WHERE ((sporty ILIKE '%%')
Why’s that anyway? Why the '%%'?
For testing the deployment I’m using Heroku with the Exceptional plugin. This plugin gives me the following hint:
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Thanks Exceptional, but what the heck does that mean? :-D Type casts for SQL queries? How’s that gonna work?
In my migration the database field looks like this:
t.boolean :sporty
And in the form where I’m creating this data I’m using this code
<%= f.label :sporty %><br />
<%= f.select :sporty, options_for_select({ "Ja" => true, "Nein" => false }), { :include_blank => '-----'} %>
As I already mentioned, SQLite is my friend, seems to be the much stricter evaluation of PostgreSQL which causes the trouble.
Thanks for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接答案就在底部附近。 。 。
在 SQL 中,“%”是通配符。但您的问题似乎是您正在构建一个具有两个左括号但只有一个右括号的 WHERE 子句。
像这样的 WHERE 子句可能会返回(或计算)所有行:
标准 SQL 有一个 CAST() 函数。骨架语法是
因此,例如,您可以编写
将时间戳更改为日期数据类型,或者
将字符串 '32' 更改为整数 32。
如果“运动”列是布尔值,那么这就是您真正的问题。如果您尝试对布尔值使用字符串比较(您这样做了: WHERE ((sporty ILIKE '%%') ),您将收到您看到的错误消息。您希望语句读起来更像这样:
或
Direct answer is near the bottom . . .
In SQL, the '%' is a wildcard. But your problem seems to be that you're building a WHERE clause that has two opening parens, but only one closing paren.
A WHERE clause like this will probably return (or count) all rows:
Standard SQL has a CAST() function. Skeleton syntax is
So, for example, you can write
to change a timestamp into a date data type, or
to change the string '32' to the integer 32.
If the column "sporty" is Boolean, this is your real problem. If you try to use a string comparison on a Boolean (which you did: WHERE ((sporty ILIKE '%%') ) you'll get the error message you saw. You want the statement to read more like these:
or