简体   繁体   English

Postgres立柱铸造

[英]Postgres column casting

I have a query 我有一个查询

SELECT assetid, type_code, version, name, short_name, status, languages,
charset, force_secure, created, created_userid, updated, updated_userid,
published, published_userid, status_changed, status_changed_userid
FROM sq_ast WHERE assetid = 7

which doesn't work and throws 这不起作用并抛出

ERROR: operator does not exist: character varying = integer LINE 4: FROM sq_ast WHERE assetid = 7

I can get it to work by doing 我可以做到这一点

SELECT assetid, type_code, version, name, short_name, status, languages,
charset, force_secure, created, created_userid, updated, updated_userid,
published, published_userid, status_changed, status_changed_userid
FROM sq_ast WHERE assetid = '7'

Please note the quoting of the 7 in the WHERE clause... 请注意在WHERE子句中引用了7。

I am deploying an huge application and I cannot rewrite the core... similarly I don't want to risk changing the type of the column... 我正在部署一个巨大的应用程序,但无法重写内核...同样,我不想冒险更改列的类型...

I'm no Postgres expert... please help... 我不是Postgres专家...请帮助...

Is there an option for strict casting of columns??? 是否可以选择严格铸造列???

Postgresql is more strongly typed in recent versions, and that's a good thing. 在最新版本中,PostgreSQL的类型更为严格,这是一件好事。 If assetid is VARCHAR, then you can't compare it to an integer (since 8.4, I believe). 如果assetid为VARCHAR,则无法将其与整数进行比较(我相信,自8.4起)。

In general (not only in Postgresql, not only in database design) it's bad design to mix those types: numeric datatypes should be used for real numeric fields, and not for strings that just happen to consist of digits. 通常(不仅在Postgresql中,不仅在数据库设计中)混合使用这些类型是不好的设计:应将数字数据类型用于实数字段,而不是用于仅由数字组成的字符串。

For example, an invoice "number", or a credit card "number", should not normally be represented as "number", but as a string. 例如,发票“数字”或信用卡“数字”通常不应表示为“数字”,而应表示为字符串。

Sometimes, however, the decision is not clear (eg: document numbers). 但是,有时决策不明确(例如:文件编号)。

Some criteria that can help: 一些标准可以帮助您:

  • Are you potentially interested in doing arithmetic with your values (sum, substract)? 您是否有兴趣对值(加,减)进行算术运算 Would that at least make sense? 那至少有意义吗? Then, it's an number . 那是个数字

  • Should zeroes on the left be preserved, or considered relevant? 是否应该保留左侧的零或将其视为相关的 (is '07' to be regarded as different from '7'?) Then, it is a string . (是否将'07'与'7'区别?)然后,它是一个字符串

Depending on how you anwser these questions in your scenario (Is there an assetid that begins with 0? Could there be some non numeric character? Does it seem to be an serial number ?), you might consider to change the field type or (more probable in your scenario) to do a cast, in the prefered direction: 根据您在方案中如何回答这些问题(是否存在以0开头的资产ID?是否可能存在一些非数字字符?它似乎是序列号吗?),您可以考虑更改字段类型或(更改更多在可能的情况下)按照偏好的方向进行投射:

   SELECT... FROM sq_ast WHERE assetid::integer = 7

(if you decide that the field is numeric) or elsewhere (如果您确定该字段为数字)或其他地方

   SELECT... FROM sq_ast WHERE assetid = '7'

There's no global setting for reverting to the old behaviour, and force an implicit cast for character types, AFAIK. 没有恢复到旧行为的全局设置,并强制对字符类型AFAIK进行隐式转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM