2013-04-13 46 views
2

我想修改Devise以使其與使用PostgreSQL的UUID主鍵的用戶表一起工作。修改設計以支持UUID主鍵

這裏是遷移:

class DeviseCreateUsers < ActiveRecord::Migration 

    def change 
    create_table :users, id: false do |t| 
     t.uuid :uuid, null: false 
     # ... 
    end 

    change_table :users do |t| 
     t.index :uuid, unique: true 
     # ... 
    end 
    end 

    def migrate(direction) 
    super 
    if direction == :up 
     # This is only necessary because the following does not work: 
     # t.uuid :uuid, primary: true, null: false 
     execute "ALTER TABLE users ADD PRIMARY KEY (uuid);" 
    end 
    end 
end 

這裏是User型號:

class User < ActiveRecord::Base 

    primary_key = :uuid 

    devise :database_authenticatable, :recoverable, :registerable, 
    :rememberable, :trackable, :validatable 

    validates :uuid, presence: true 
    before_validation :ensure_uuid 
    def ensure_uuid; self.uuid ||= SecureRandom.uuid end 

end 

以下是錯誤:

PG::Error: ERROR: operator does not exist: uuid = integer 
LINE 1: ...ECT "users".* FROM "users" WHERE "users"."uuid" = 1 ORDER... 
                  ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
: SELECT "users".* FROM "users" WHERE "users"."uuid" = 1 ORDER BY "users"."uuid" ASC LIMIT 1 

Extracted source (around line #5): 

1 .navbar-inner 
2  .container 
3  = a_nav_tag "App", root_path 
4  - if user_signed_in? 
5   %ul.nav.pull-right 
6   %li.dropdown#user_menu 
7    %a.dropdown-toggle(data-toggle="dropdown" href="#") 

正如你可以在上面看到,user_signed_in?被打破。我希望有一些變化需要從「常規」自動遞增ID移動到UUID。

現在,我只是發佈這個問題。我今天晚些時候會對此表示震驚。如果你碰巧知道如何做到這一點 - 或者瞭解設計分支,我會很感激。

回答

3

只要清除您的瀏覽器的網絡應用程序的cookie(在我的情況下,localhost)。上述錯誤是由於會話保留舊用戶主鍵1而引起的。

之後,事情在我的測試工作。我希望這不僅僅是運氣,如果Devise不知道主鍵,這將是一個很好的設計。 (在Devise的代碼中,除了在某些測試中,我看到沒有使用.id)。

8

我已經在Rails 4中完成了這項工作,只需在創建表時將id列設置爲uuid數據類型,並且沒有任何其他配置更改。即。不要創建名爲'uuid'的列,只需將'id'列的類型更改爲uuid。