LoginSignup
1
0

【マイグレーションファイル】削除の方法【rails】

Posted at

マイグレーションの削除で6時間ほどかかってしまったため今後の備忘録として。

DBeaver にテーブルが登録されないため
一度手動で消してしまったのが運の尽き。

VSコードのマイグレーションは難なく消去できたがDBeaver 内は外部キーがあったせいで削除できず。:skull:

しかし解決策として
・マイグレーションファイルの順序を変更
・NO FILEの解消
が解決策として有効だと学んだ

マイグレーションファイルの順序として
メイン、親となるものが先にくることが判明

例えば

class CreateOrders < ActiveRecord::Migration[7.0]
  def change
    create_table :orders do |t|
      t.references :item_id,  null: false, foreign_key: true
      t.references :user_id,  null: false, foreign_key: true
      t.timestamps
    end
  end
end


class CreateAddresses < ActiveRecord::Migration[7.0]
  def change
    create_table :addresses do |t|
      t.string :zip_code,                                    null: false
      t.integer :region_of_shipping_origin_id,            null: false
      t.string :city,                                        null: false
      t.string :street_address,                              null: false
      t.string :apartment_name,                              null: false
      t.string :tel,                                         null: false
      t.references :order,                                   null: false, foreign_key: true
      t.timestamps
    end
  end
end

orderのreferenceとなっているため
orderが先に来るらしい

ーー次にNO FILEの解消方法ーー

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20240119065421  Devise create users
   up     20240206035300  Create active storage tablesactive storage
   up     20240206055210  Create items
   up     20240227022924  ********** NO FILE **********
   up     20240227023037  Create addresses
  down    20240229011710  Create orders

まず削除したmigrantionIDの名前で再度ファイルをマイグレーション内に作る
ファイル内に

class CreateOrders < ActiveRecord::Migration
  def change
  end
end

としていたがロールバックできず。
原因は

class CreateOrders < ActiveRecord::Migration[7.0]
  def change
  end
end

7.0がなかったことが原因でした

その後

 rails db:rollback

の後、VSコード内migration内の該当するファイルを今度こそ手動で削除して完了!!

自己満かつ備忘録なので分かりにくいかと思いますがそこはご愛敬で:relaxed:

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