Jack
Jack

Reputation: 1163

PGError: operator does not exist: character varying = bigint

I'm trying to interact with the Twitter API to show a user_timeline on my website.

I followed the railscasts.com video for Twitter integration: http://railscasts.com/episodes/359-twitter-integration

I am interacting with the API just fine, pulling the information into my application and it is displaying and working in development.

My code is as follows:

Model - 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

Here's the migration:

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

end end

And to display the Tweets:

<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>

The idea is to store the tweets in the database so that even if Twitter is down, this won't affect the users seeing the most recent tweets.

Anyway, when I run the command Timeline.pull_tweets in the console it works fine.

It's when I push to heroku, migrate the db, and attempt to run the same command.

Then I get this error:

  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.

Any help with what is going on?

I've also tried to run the migration so that :tweet_id is an integer, but I got another error on heroku as well.

Thanks.

Upvotes: 0

Views: 4717

Answers (1)

mu is too short
mu is too short

Reputation: 434685

You've created tweet_id as a string (AKA varchar(255) in PostgreSQL):

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

but your tweet.id:

unless exists?(tweet_id: tweet.id)

is actually a number. If you want to keep storing your tweet_id as a string then you'll have to convert the id to a string everywhere that you use it:

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

If you want to fix your production table to use an integer for tweet_id instead of your current string, you have a couple options:

  1. Drop and re-create the table with the correct schema. This will work fine but you'll lose any data you have.
  2. Manually issue an ALTER TABLE so that you can use USING to tell PostgreSQL how to convert your strings to integers.

Once you have that figured out, you should install PostgreSQL locally and develop on top of PostgreSQL if you're planning on deploying to Heroku.

Upvotes: 3

Related Questions