2

PostgreSQL で UUID プライマリ キーを持つ users テーブルで動作するように Devise を変更したいと考えています。

移行は次のとおりです。

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 に移行するには、いくつかの変更が必要になると思います。

今のところ、私は質問を投稿しているだけです。これについては、今日の後半にスイングします。たまたまこれを行う方法を知っていたり、Devise のフォークを知っていたりしたら、私は感謝します。

4

3 に答える 3

3

Web アプリのブラウザーの Cookie をクリアするだけです (私の場合はlocalhost)。上記のエラーは、セッションが古いユーザーの主キー を保持していたために発生します1

その後、私のテストではうまくいきます。これが単なる運ではなく、Devise が主キーにとらわれない設計になっていることを願っています。.id(Devise のコードでは、いくつかのテストで except の使用は見られませんでした。)

于 2013-04-13T22:16:30.207 に答える