0
0

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 3 years have passed since last update.

rails:db:migrateを実行すると「Directly inheriting from ActiveRecord::Migration is not supported.」と出た

Posted at

<この記事について>
自作のアプリ製作中にカラムを追加する流れで「rails:db:migrate」を実行すると「Directly inheriting from ActiveRecord::Migration is not supported.」と表示されエラーに。
初歩的なミスでしたが備忘録として投稿!

[環境]
・Ruby 2.6.5,
・Rails 6.0.0
・macOS

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

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

調べてみると、原因はマイグレーションファイルがrailsのバージョンに対応していないことが原因のようでした。。

対象のマイグレーションファイルの1行目末尾にrailsのバージョンを追加。

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:migarate

<最後に>
実装してエラーが出る、思うような結果が得られないことは多々あると思います。
そんな時は冷静な気持ちでエラー文と丁寧に向き合うようにしましょう!

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?