LoginSignup
1
0

More than 3 years have passed since last update.

railsのエラー: データベースの名前

Last updated at Posted at 2019-11-04

railsのエラーの備忘録です。

カラム名では予約後は使えない

railsでは予約語が設定されておりシステム上使えないことになっています。
エラー内容はこんな感じで

ActiveRecord::SubclassNotFound (The single-table inheritance mechanism failed to locate the subclass: 'news'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Site.inheritance_column to use another column for that information.):
app/controllers/sites_controller.rb:5:in `index'

予約語の例として
reload
send
methods
from_xml
destroy
load
hash
class
touch
errors
changed
try
save
inspect
type
など使うとエラーが出るので注意が必要です。

対応方法

単純にカラム名を変更するだけです。

その1、ターミナル下記のコマンドを打ち
ails generate migration rename_[変更前のカラム名]column_to[モデル名(複数形)]

その2、マイグレーションファイルを下記のように編集し
def change
rename_column :テーブル名, :変更前のカラム名, :変更後のカラム名
end

その3、あとはいつもどりmigrateです。

rake db:migrate

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