PGError:错误:运算符不存在:布尔值 ~~* 未知

发布于 2024-10-13 02:20:53 字数 1027 浏览 1 评论 0原文

在本地,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-10-20 02:20:53

直接答案就在底部附近。 。 。

计算结果为

SELECT COUNT(*) FROM "customers" WHERE ((sporty ILIKE '%%') 

为什么会这样?为什么是“%%”?

在 SQL 中,“%”是通配符。但您的问题似乎是您正在构建一个具有两个左括号但只有一个右括号的 WHERE 子句。

像这样的 WHERE 子句可能会返回(或计算)所有行:

WHERE (sport ILIKE '%%')

SQL 查询的类型转换?怎么样
会工作吗?

标准 SQL 有一个 CAST() 函数。骨架语法是

CAST (expression AS type)

因此,例如,您可以编写

CAST (<any timestamp> AS DATE) 

将时间戳更改为日期数据类型,或者

CAST ('32' AS INTEGER)

将字符串 '32' 更改为整数 32。

在我的迁移中数据库字段
看起来像这样:

t.boolean :sporty

如果“运动”列是布尔值,那么这就是您真正的问题。如果您尝试对布尔值使用字符串比较(您这样做了: WHERE ((sporty ILIKE '%%') ),您将收到您看到的错误消息。您希望语句读起来更像这样:

SELECT COUNT(*) FROM "customers" WHERE sporty;
SELECT COUNT(*) FROM "customers" WHERE sporty = true;

SELECT COUNT(*) FROM "customers" WHERE NOT sporty;
SELECT COUNT(*) FROM "customers" WHERE sporty = false;

Direct answer is near the bottom . . .

This evaluates to

SELECT COUNT(*) FROM "customers" WHERE ((sporty ILIKE '%%') 

Why’s that anyway? Why the '%%'?

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:

WHERE (sport ILIKE '%%')

Type casts for SQL queries? How’s that
gonna work?

Standard SQL has a CAST() function. Skeleton syntax is

CAST (expression AS type)

So, for example, you can write

CAST (<any timestamp> AS DATE) 

to change a timestamp into a date data type, or

CAST ('32' AS INTEGER)

to change the string '32' to the integer 32.

In my migration the database field
looks like this:

t.boolean :sporty

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:

SELECT COUNT(*) FROM "customers" WHERE sporty;
SELECT COUNT(*) FROM "customers" WHERE sporty = true;

or

SELECT COUNT(*) FROM "customers" WHERE NOT sporty;
SELECT COUNT(*) FROM "customers" WHERE sporty = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文