One case of solving a migration error in Rails

This time, I failed in the migration, consulted with the mentor, and wrote a memo until the solution. I was making something like an app that counts likes with online school materials. I put in the code to count the likes and ran the migration, but apparently it didn't work.

Migration error occurred

When you run rails db: migrate,

An error has occurred, this and all later migrations canceled:PG::UndefinedColumn: ERROR: column "likes_count" of relation "posts" does not existLINE 1: UPDATE "posts" SET "likes_count" = $1 WHERE "posts"."id" = $... I got the error.

It was Google Translate, but it seemed to be angry that "likes_count does not exist in the post column!".

Check rails db: migrate: status

I received an instruction from a mentor saying "Please check rails db: migrate: status. " When I check it,

database: like_app_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20201221071016  Devise create users
   up     20201221072319  Create posts
   up     20201221223916  Create likes
   up     20201222001044  Add likes count to posts
  down    20201222001313  Reset all post cache counters

Apparently, the likes count wasn't added well in the 20201222001044 part.

rails db: rollback

File in question,

20201222001044_add_likes_count_to_posts.rb


class AddLikesCountToPosts < ActiveRecord::Migration[6.1]
  def change
    add_column :posts, :likes_count, :integer, default: 0
  end
end

Comment this out and roll back using the rails db: rollback command. Then comment out and run rails db: migrate again. The migration was done correctly, and we were able to confirm the operation properly! !! : clap :: clap ::

Summary

I think the failure this time was probably because I had migrated before putting in likes_count. When migrating, I would like to confirm the files properly before executing. Even so, I was able to solve it immediately with the quick response of the mentor. Thank you, thank you!

Recommended Posts

One case of solving a migration error in Rails
[Rails] Solution when migration error occurs in acts-as-taggable-on
Understand migration in rails
Migration error after Activerecord association in Rails5 + Docker environment (2)
Migration error after Activerecord association in Rails5 + Docker environment
Error in rails db: migrate
[Rails] Japanese localization of error messages
Add a search function in Rails.
[Rails] Unexpected validation error in devise
Create a new app in Rails
Check the migration status of rails
Japanese localization of error messages (rails)
Implement a contact form in Rails
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
[Rails] I want to send data of different models in a form
How to implement a slideshow using slick in Rails (one by one & multiple by one)
A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
How to insert a video in Rails
The identity of params [: id] in rails
[rails] List of actions defined in Controller
Steps to set a favicon in Rails
Rails Basics of creating a new application
English translation of Pleiades All in One
Rails migration
It's just now, but a collection of commands that frequently appear in Rails
If you use Spring's DataSourceTransactionManager, it may be committed in case of error! ??
[Rails] How to get rid of flash messages in a certain amount of time
How to make a unique combination of data in the rails intermediate table
[Order method] Set the order of data in Rails
Rails: A brief summary of find, find_by, where
Convert to a tag to URL string in Rails
A summary of only Rails tutorial setup related
Stuck in an enum in front of a blacksmith
Error in bundle install when running rails new
Measure the size of a folder in Java
Recommendation of Service class in Ruby on Rails
[Rails] Implementation of retweet function in SNS application
[Rails] Japanese localization of validation error message ~ ja.yml ~
Rails logger Get a rough idea in 1 minute
[Ruby on Rails] A memorandum of layout templates
[Rails] Implementation of "notify notification in some way"
One way to redirect_to with parameters in rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
A quick review of Java learned in class
500 Internal Server Error occurs in Rails production environment
How to use JQuery in js.erb of Rails6
(Ruby on Rails6) Creating data in a table
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
How to make a follow function in Rails
[Rails] ActiveRecord :: HasManyThrough Order Error in Users # show
Create a native extension of Ruby in Rust
[Rails] Ranking and pagination in order of likes
[Ruby on Rails] Individual display of error messages
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
What to do if you get a wrong number of arguments error in binding.pry
A story that got stuck with an error during migration in docker PHP laravel
[Rails] I want to display the link destination of link_to in a separate tab