29
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

unknown attribute '○○○○' for ○○○. →カラムのリネーム

Last updated at Posted at 2019-10-19

#エラー発生

環境:rails 6

タスクアプリを練習として作っていて、新規登録の実装をして
画面を開いて、動作確認するとこんなエラーが
unknown attribute 'description' for Task.
「descriptionがTaskオブジェクトで見つからない~」

#DB確認
DBを見てみよう
ターミナルを開いて

$rails c
$Task.new
=> #<Task id: nil, name: nil, descriprion: nil, ~ >

カラム名が違う!

#カラム名修正
モデル:task
カラム変更前:descriprion
カラム変更後:description

##1.マイグレーションファイル準備
$ rails generate migration rename_descriprion_column_to_tasks
(rails generate migration rename_変更前のカラム名_column_to_モデル名複数形)

db/migrate/XXXXXXXXXXX_rename_descriprion_column_to_tasks.rb
というファイルが生成されます

##2.ファイル編集
rename_column テーブル名, 変更前のカラム名, 変更後のカラム名 といった感じで編集します。

XXXXXXXXXXX_rename_descriprion_column_to_tasks.rb
class RenameDescriprionColumnToTasks < ActiveRecord::Migration[6.0]
  def change
    rename_column :tasks, :descriprion, :description
  end
end

##3.DBに反映

$ rake db:migrate

29
18
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
29
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?