2011-12-14 63 views
2

我有一个适用于Rails应用程序的PostgreSQL数据库。Rails和PostgreSQL中用于Facebook用户标识的数据类型

我想存储Facebook的用户ID,所以我想我可以使用整数,但它不够大,所以我选择浮动。

但是现在Rails添加1.0到年底我的用户ID的

我可以使用哪些数据类型,所以不会发生这种情况对于Facebook用户ID,这是非常长的example: 100002496803785

回答

7

您可以在整数列上使用:limit => 8来获得bigint。例如:

class Pancakes < ActiveRecord::Migration 
    def change 
     create_table :pancakes do |t| 
      t.integer :c, :limit => 8 
     end 
    end 
end 

,然后从psql

=> \d pancakes 
         Table "public.pancakes" 
Column | Type |      Modifiers      
--------+---------+------------------------------------------------------- 
id  | integer | not null default nextval('pancakes_id_seq'::regclass) 
c  | bigint | not null 
Indexes: 
    "pancakes_pkey" PRIMARY KEY, btree (id) 

而且还有你的eight byte bigint列。

您也可以使用Facebook ID的字符串。你没有对这些ID进行任何算术运算,因此它们实际上只是不透明的包,看起来像大整数,字符串会进行排序和比较,所以它们可能是最好的选择。由于整数字符串的大小增加,会有一些存储和访问开销,但它可能不足以产生任何显着差异。

从来没有使用double的东西需要确切。在这种情况下,你可能会很好(除了尾部的.0),因为你有52位尾数,这意味着double会像52位整数一样,直到你的值变得足够大,需要指数。即使如此,使用double这将是一个可怕的想法和类型系统的滥用。

0

我不使用PostgreSQL但在mysql中我使用BIGINT

根据postgresql data types,BIGINT for postgresql也是如此。

+0

然而轨没有大INT。 – chell 2011-12-14 10:52:50

+0

我以为在铁轨上有什么高贵的东西,不是吗? – danielv 2011-12-14 11:21:29

0

mu太短有一个很好的答案,我只想补充一点,如果你想使用ID作为表之间的外键,那么你应该坚持他描述的BIGINT解决方案,而不是使用字符串。这是我用的,主要有:

例子:

create_table(:photos) do |t| 
    t.integer  :fb_uid, :limit => 8 # Facebook ID of the photo record 
    t.integer  :facebook_profile_uid, :limit => 8, :null => false # foreign key to user 
    # ... 
end 

create_table(:users) do |t| 
    t.integer  :fb_uid, :limit => 8, :null => false # Facebook ID of the user record 
    t.integer  :photos_count, :integer, :default => 0 
    # ... 
end 

class User < ActiveRecord::Base 
    has_many :photos, foreign_key: :facebook_profile_uid, primary_key: :fb_uid 
    # ... 
end 

class Photo < ActiveRecord::Base 
    belongs_to :facebook_profile, foreign_key: :facebook_profile_uid, primary_key: :fb_uid, :counter_cache => true 
end 
相关问题