Facebook User IDs are BigInts in PostgreSQL

We’ve recently been working on integrating 5 Blocks Out with Facebook, starting with making it easy to sign up via Facebook Connect. I stumbled across a bug related to this today that might impact other developers doing the same thing, so I figured it would be worth sharing.

When someone signs up on 5 Blocks Out using their Facebook account, we take their Facebook user ID (fb_uid) and save it in a database table so we can remember them later. We use PostgreSQL as our database.

Now, Facebook user IDs are integer values, and it turns out they can be pretty big. Bigger, in fact, than the default “integer” type which Rails migrations use when creating an integral-valued column in a PostgreSQL table. So if you follow the defaults, you will eventually crash when a user with a large fb_uid tries to sign up.

My code was doing something like this…

user = User.find_by_fb_uid(current_facebook_user.id)

… and with current_facebook_user.id too large, you get this unhappy result:

ERROR: value \"1000001234556789\" is out of range for type 
integer\n: SELECT * FROM \"users\" WHERE
(\"users\".\"fb_uid\" = E'1000001234556789') LIMIT 1

The solution is to use type :bigint when creating your database column. Or, as in our case, change your existing integer column from integer to bigint:

class RecoverFromDumbAssedProductionError < ActiveRecord::Migration  
  def self.up    
    change_column(:users, :fb_uid, :bigint)  
  end
end

Happy Facebooking.

Follow

Get every new post delivered to your Inbox.

Join 401 other followers

%d bloggers like this: