2012-07-01 61 views
0

我正在嘗試與Twitter API交互以在我的網站上顯示user_timeline。PGError:操作符不存在:character varying = bigint

我跟着Twitter的整合railscasts.com視頻:http://railscasts.com/episodes/359-twitter-integration

我與API就好了互動,拉動信息到我的應用程序,它會顯示在開發工作。

我的代碼如下:

型號 - timeline.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上又遇到了一個錯誤。

謝謝。

回答

2

您已經創建了tweet_id作爲字符串(AKA varchar(255) PostgreSQL中):

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如何將字符串轉換爲整數。

一旦你明白了,你應該在本地安裝PostgreSQL並在PostgreSQL之上開發,如果你打算部署到Heroku。

+0

我真的很想試試,但我厭倦了推動Heroku多次測試。永遠。使用to_s工作和控制檯命令在Heroku上工作。非常感謝,並且我計劃在本地安裝PostgreSQL。剛拿到一臺Mac w/Retina,無法讓Postgre安裝在我的2008 Mac上工作。再次感謝 – Jack

相關問題