我正在尝试将一个简单的 Sinatra 应用程序迁移到 Heroku。使用 Taps 迁移 Ruby 应用程序代码和现有 MySQL 数据库很顺利,但我收到以下 Postgres 错误:
PostgresError - ERROR: operator does not exist: text = integer LINE 1: ...d_at", "post_id" FROM "comments" WHERE ("post_id" IN (4, 17,... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
很明显,问题与查询中的类型不匹配有关,但这是由 DataMapper ORM 在非常高的抽象级别从 Haml 模板发出的,所以我不确定我该怎么做关于控制这个...
具体来说,这似乎是在我的 Haml 模板调用 p.comments
时抛出,其中 p
代表给定的帖子。
Datamapper 模型相关如下:
class Post
property :id, Serial
...
has n, :comments
end
class Comment
property :id, Serial
...
belongs_to :post
end
这在我的本地和当前使用 MySQL 的托管环境中运行良好,但 Postgres 显然更严格。
一定有数百个 Datamapper 和 Haml 应用程序在 Postgres 数据库上运行,并且这种模型关系非常传统,所以希望有人已经看到(并确定如何修复)它。谢谢!
更新:参见 Heroku: Postgres type operator error after migrating DB from MySQL寻求解决。
最佳答案
看起来 post_id 是 TEXT 类型而不是 INTEGER。要解决此问题,您必须更改数据类型。这在版本 8.3 中已更改,旧版本具有隐式转换。您可以告诉 PostgreSQL 这样做:
CREATE FUNCTION pg_catalog.text(integer) RETURNS text STRICT IMMUTABLE LANGUAGE SQL AS 'SELECT textin(int4out($1));';
CREATE CAST (integer AS text) WITH FUNCTION pg_catalog.text(integer) AS IMPLICIT;
CREATE FUNCTION pg_catalog.text(smallint) RETURNS text STRICT IMMUTABLE LANGUAGE SQL AS 'SELECT textin(int2out($1));';
CREATE CAST (smallint AS text) WITH FUNCTION pg_catalog.text(smallint) AS IMPLICIT;
CREATE FUNCTION pg_catalog.text(bigint) RETURNS text STRICT IMMUTABLE LANGUAGE SQL AS 'SELECT textin(int8out($1));';
CREATE CAST (bigint AS text) WITH FUNCTION pg_catalog.text(bigint) AS IMPLICIT;
另见 http://wiki.postgresql.org/wiki/Image:Pg83-implicit-casts.sql
关于postgresql - Heroku 上 Sinatra/Haml/DataMapper 的 Postgres 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2935985/