【问题标题】:PGError: operator does not exist: character varying = bigintPGError:运算符不存在:字符变化= bigint
【发布时间】:2012-07-02 10:37:41
【问题描述】:

我正在尝试与 Twitter API 交互以在我的网站上显示 user_timeline。

我关注了 railscasts.com 的 Twitter 集成视频:http://railscasts.com/episodes/359-twitter-integration

我可以很好地与 API 交互,将信息提取到我的应用程序中,它正在显示并在开发中工作。

我的代码如下:

模型 - 时间线.rb

class Timeline < ActiveRecord::Base
  attr_accessible :content, :screen_name, :tweet_id

  def self.pull_tweets
   Twitter.user_timeline("{username_goes_here}", since_id: maximum(:tweet_id)).each do |tweet|
    unless exists?(tweet_id: tweet.id)
      create!(
        tweet_id: tweet.id,
        content: tweet.text,
        screen_name: tweet.user.screen_name,
      )
    end
  end
end
end

这里是迁移:

class CreateTimelines < ActiveRecord::Migration
  def change
    create_table :timelines do |t|
     t.string :tweet_id
     t.string :screen_name
     t.text :content

     t.timestamps
end

结束 结束

并显示推文:

<div id="timeline">
      <% Timeline.order("tweet_id desc").limit(3).each do |timeline| %>
         <h3><%= timeline.content %></h3>

        <p class="pull-right">
            ~ @<%= timeline.screen_name %>
        </p>
      <% end %>
    </div>

这个想法是将推文存储在数据库中,这样即使 Twitter 关闭,也不会影响查看最新推文的用户。

无论如何,当我在控制台中运行命令Timeline.pull_tweets 时,它工作正常。

当我推送到 heroku、迁移数据库并尝试运行相同的命令时。

然后我得到这个错误:

  PGError: ERROR:  operator does not exist: character varying = bigint
LINE 1: ...ne FROM "timelines"  WHERE "timelines"."tweet_id" = 21919081...
                                                         ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

有什么帮助吗?

我还尝试运行迁移,以便 :tweet_id 是一个整数,但我在 heroku 上也遇到了另一个错误。

谢谢。

【问题讨论】:

    标签: ruby-on-rails sqlite postgresql heroku twitter


    【解决方案1】:

    您已将 tweet_id 创建为字符串(在 PostgreSQL 中也称为 varchar(255)):

    create_table :timelines do |t|
      t.string :tweet_id
    

    但是你的tweet.id:

    unless exists?(tweet_id: tweet.id)
    

    实际上是一个数字。如果您想继续将 tweet_id 存储为字符串,那么您必须在使用它的任何地方将 id 转换为字符串:

    unless exists?(tweet_id: tweet.id.to_s)
      create!(
        tweet_id: tweet.id.to_s,
        ...
    

    如果您想修复生产表以使用整数表示 tweet_id 而不是当前字符串,您有几个选择:

    1. 删除并重新创建具有正确架构的表。这可以正常工作,但您会丢失所有数据。
    2. 手动发出ALTER TABLE so that you can use USING 告诉 PostgreSQL 如何将字符串转换为整数。

    一旦你弄清楚了,如果你打算部署到 Heroku,你应该在本地安装 PostgreSQL 并在 PostgreSQL 之上开发。

    【讨论】:

    • 我本来只是想尝试一下,但我厌倦了多次推向 Heroku 进行测试。需要永远。使用 to_s 有效,并且控制台命令在 Heroku 上有效。非常感谢,是的,我计划在本地安装 PostgreSQL。刚买了一台带 Retina 的 Mac,无法让 Postgre 安装在我的 2008 Mac 上工作。再次感谢
    猜你喜欢
    • 2021-12-09
    • 2021-11-12
    • 2016-04-16
    • 2012-02-06
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    相关资源
    最近更新 更多