Open1

rails カラム名変更でうまくいかない

pipopipo

カラム名変更

docker-compose run web rails g migration rename_post_id_column_to_likes  
docker-compose run web rails db:migrate

でも変わっていない・・・

schema.rb
  create_table "likes", force: :cascade do |t|
    t.integer "user_id"
    t.integer "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

migrateうまくいってる?

docker-compose run web rails db:migrate:status
[+] Running 1/0
 - Container docker_demo-db-1  Running                                                                                                              0.0s 

database: docker_demo_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20230117072632  Create tasks
   up     20230119040604  Change tasks name not null
   up     20230119041422  Change tasks name limit30
   up     20230119055900  Create users
   up     20230119063922  Add admin to users
   up     20230120051851  Add user id to tasks
   up     20230126041105  Create active storage tablesactive storage
   up     20230201040719  Create tweets
   up     20230203035705  Create likes
   up     20230203051916  Rename post id column to likes

実行できてるの反映されていない・・

まだデータは入っていないのでカラム追加して削除します。

 docker-compose run web rails g migration AddColumnLikes
20230203055623_add_column_likes.rb
class AddColumnLikes < ActiveRecord::Migration[7.0]
  def change
    add_column :likes, :tweet_id, :string, null: false
  end
end
 docker-compose run web rails g migration RemovePostIdFromLikes post_id:integer
 docker-compose run web rails db:migrate

途中で型も間違っていたので修正

 docker-compose run web rails g migration ChangeDataUserIdToLike
20230203060657_change_data_user_id_to_likes.rb
class ChangeDataUserIdToLikes < ActiveRecord::Migration[7.0]
  def change
    change_column :likes, :user_id, :string
  end
end
 docker-compose run web rails db:migrate

修正完了。

schema.rb
  create_table "likes", force: :cascade do |t|
    t.string "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "tweet_id", null: false
  end

変更できたので良しとします。