【发布时间】:2011-02-25 23:40:42
【问题描述】:
这是我之前提出的问题的后续,该问题将其表述为编程问题而不是数据库问题。
Postgres error with Sinatra/Haml/DataMapper on Heroku
我认为问题已被隔离到运行 db:push 后 Heroku 的 Postgres 数据库中 ID 列的存储。
简而言之,我的应用程序在我的原始 MySQL 数据库上正常运行,但在对 ID 列执行任何查询时在 Heroku 上引发 Postgres 错误,这似乎已作为 TEXT 存储在 Postgres 中,即使它在 MySQL 中存储为 INT .我的问题是为什么在 Postgres 中将 ID 列创建为 INT 以将数据传输到 Heroku,以及我是否有任何方法可以防止这种情况发生。
以下是 heroku console 会话的输出,它演示了该问题:
Ruby console for myapp.heroku.com
>> Post.first.title
=> "Welcome to First!"
>> Post.first.title.class
=> String
>> Post.first.id
=> 1
>> Post.first.id.class
=> Fixnum
>> Post[1]
PostgresError: ERROR: operator does not exist: text = integer
LINE 1: ...", "title", "created_at" FROM "posts" WHERE ("id" = 1) ORDER...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Query: SELECT "id", "name", "email", "url", "title", "created_at" FROM "posts" WHERE ("id" = 1) ORDER BY "id" LIMIT 1
谢谢!
【问题讨论】:
-
列 ID 看起来像数据类型 TEXT,而不是 INTEGER。否则不会有这个问题。
-
是的,就是这样。挑战在于如何在 Heroku 上的数据库部署限制范围内防止这种情况发生。
-
在常见问题解答中:docs.heroku.com/database#frequently-asked-questions,请参阅无效运算符。
-
我以前读过,虽然它确实提供了一种成功查询当前数据库结构的方法(即
Post.all(:conditions => {:id => "1"})),但它并没有解决获取 ID 的核心问题要创建为 INTEGER 的列。
标签: postgresql heroku datamapper taps