2011-12-14 66 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 
相關問題