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?

【Error・豆知識】マイグレーションうまくいかない!? #はつけないほうがいいのか??

Last updated at Posted at 2024-09-09

はじめに

今回はマイグレーションファイルに必要事項を入力した後、マイグレーション実行しようと思ったらうまくいかなかったので、解決までの流れを後の為にも、記録として残しておくために記事にしたいと思います。

環境

  • Windows, WSL
  • Docker
  • Ruby 3.2.3
  • Rails 7.1.3

実際に入力したマイグレーションファイルの中身

ユーザー認証機能を実装中の事。
割と初期段階ですね。
#をつけてどんな内容なのかわかりやすくして入力していました。

class SorceryCore < ActiveRecord::Migration[7.2]
  def change
    create_table :users do |t|
      t.string :email,            null: false, index: { unique: true }
      t.string :crypted_password
      t.string :salt

      t.string :name, null: false # ユーザーの名前
      t.string :avatar            # アバター画像
      t.integer :age              # 年齢を保存
      t.string :gender            # 性別を保存(例: '女性', '男性', 'その他')
      t.string :experience_level  # エンジニア歴を保存
      t.string :occupation        # 職業を保存(例: '学生', '社会人', 'フリー')

      t.timestamps                null: false
    end
  end
end

そしてマイグレーション実行

$ docker compose exec web rails db:migrate

db/schema.rbを確認したら次のようになっていました
明らかに何かしらすくない状態です。

ActiveRecord::Schema[7.2].define(version: 2024_09_09_044009) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "users", force: :cascade do |t|
    t.string "email", null: false
    t.string "crypted_password"
    t.string "salt"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
  end
end

ここでマイグレーションファイルがupになっているか確認してみたところ、up状態になっていました。

docker compose exec web rails db:migrate:status

修正してみる

# 最後に実行したマイグレーションをロールバック
docker compose exec web rails db:rollback STEP=1

先ほどのマイグレーション実行後のusersテーブルの内容から、この「#」が入っていたものがテーブルに反映されていなかったように感じたので#を消してみました

class SorceryCore < ActiveRecord::Migration[7.2]
  def change
    create_table :users do |t|
      t.string :name, null: false
      t.string :email,            null: false, index: { unique: true }
      t.string :crypted_password
      t.string :avatar
      t.integer :age
      t.string :gender
      t.string :experience_level
      t.string :occupation
      t.string :salt

      t.timestamps                null: false
    end
  end
end
# その後、再度マイグレーションを実行
docker compose exec web rails db:migrate
  create_table "users", force: :cascade do |t|
    t.string "name", null: false
    t.string "email", null: false
    t.string "crypted_password"
    t.string "avatar"
    t.integer "age"
    t.string "gender"
    t.string "experience_level"
    t.string "occupation"
    t.string "salt"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
  end

反映された!!

さいごに

おそらくマイグレーションファイルの内容を#で表示しようとしたのは私だけかもしれません。ですが、今回のなかなかマイグレーション実行がうまくいかないことから学んだものは大きかった、はず。
今回の記事が何か参考になれば幸いです。

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?