LoginSignup
1
0

More than 3 years have passed since last update.

TABLEの主キーをuuid型に変更した際にNo route matchesのエラーが出た。

Posted at

環境

  • Rails 5.2.3
  • psql (PostgreSQL) 11.4

事象

  • DBに登録したユーザーのレコードを取得するURLにアクセスした。
  • 画像のとおり、No route matchesのエラーが表示された。

スクリーンショット 2019-09-19 19.49.34.png

原因

テーブルの主キーの型をbigintからuuidに変更したのに、テーブルのcolumn name、routes.rbを修正しなかったため。
スクリーンショット 2019-09-19 19.52.30.png

対応

①該当テーブルのcolumn nameをuuidに変更

class RenameHogeModelPrimaryKey < ActiveRecord::Migration[5.2]
  def change
    rename_column :hoges, :id, :uuid
  end
end

②対応するモデルに主キーがuuidであることを記述

class Hoge < ApplicationRecord
  self.primary_key = "uuid"

③routes.rbにてparamをidからuuidに変更した

resources :hoges, param: :uuid
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0