title: Rails 5: How can I change an existing model's ID type to UUID?
body: <p>I'm trying to change a Devise User model's id type to a uuid.</p>
  
  <p>My migration looks like this:</p>
  
  <pre><code>class ChangeUserIdTypeToUuid &lt; ActiveRecord::Migration[5.2]
  
    def up
      change_column :users, :id, :uuid
    end
  
    def down
      change_column :users, :id, :integer
    end
  end
  </code></pre>
  
  <p>But when I run the migration I get an error:</p>
  
  <pre><code>== 20180909205634 ChangeUserIdTypeToUuid: migrating ===========================
  -- change_column(:users, :id, :uuid)
  rails aborted!
  StandardError: An error has occurred, this and all later migrations canceled:
  
  PG::DatatypeMismatch: ERROR:  column "id" cannot be cast automatically to type uuid
  HINT:  You might need to specify "USING id::uuid".
  : ALTER TABLE "users" ALTER COLUMN "id" TYPE uuid
  </code></pre>
  
  <p>There's a hint in there but I don't know what it's suggesting I do. It's not this:</p>
  
  <pre><code>change_column :users, :id, id::uuid
  </code></pre>
  
  <p>Why is the migration failing? What is the hint suggesting? How do I change the ID type to UUID?</p>