LoginSignup
11
4

More than 3 years have passed since last update.

[Rails6.0]rails db:migrateをする際に発生したDirectly inheriting from ActiveRecord::Migration is not supported.の解決法

Posted at

gemのpaperclipを使うためrails g paperclip post imageを実行してマイグレーションファイルを生成。

その後rails db:migrateしたら

rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

というエラーが発生。

以下のサイトを参考にしたところ、

【rails5.2.0】rails db:migrateをする際に発生したDirectly inheriting from ActiveRecord::Migration is not supported.の対処法について

どうやら原因はマイグレーションファイルがrailsのバージョンに対応していないことが原因のようだった。

解決法

参考サイトではRails5.2の話だったが、6.0でも通用するのか試しにやってみた。

20200127214811_add_attachment_image_to_posts.rb
class AddAttachmentImageToPosts < ActiveRecord::Migration[6.0] #←[6.0]を追加
  def self.up
    change_table :posts do |t|
      t.attachment :image
    end
  end

  def self.down
    remove_attachment :posts, :image
  end
end

これでrails db:migrateをしたら無事成功しました。

学んだこと

マイグレーションファイルがRailsのバージョンにたいおうしていなかったら、ファイルのクラス部分にバージョンを指定してあげる。

11
4
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
11
4