Search code examples
ruby-on-railsrubyrails-migrationsruby-on-rails-7

how to resolve pending migration error in rails 7


I have created a migration in which i mistakenly duplicate the columns of "created_at" , now my screen is displaying this error, after when i remove the duplication, the errror was also same before removing the duplication.

This is the terminal messages:

PS C:\Users\iamvee_k\Desktop\Rails Projects\blog> rails db:migrate
== 20231003192318 AddTimestampsToArticles: migrating ==========================
-- add_column(:articles, :created_at, :datetime)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: created_at
C:/Users/iamvee_k/Desktop/Rails Projects/blog/db/migrate/20231003192318_add_timestamps_to_articles.rb:3:in `change'

Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: duplicate column name: created_at
C:/Users/iamvee_k/Desktop/Rails Projects/blog/db/migrate/20231003192318_add_timestamps_to_articles.rb:3:in `change'

Caused by:
SQLite3::SQLException: duplicate column name: created_at
C:/Users/iamvee_k/Desktop/Rails Projects/blog/db/migrate/20231003192318_add_timestamps_to_articles.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

and this is the output screen: enter image description here


Solution

  • You have created new migration but not yet migrated to your DB as well as there is an duplicate column exception was raised, so just use

    bundle exec rake db:drop
    bundle exec rake db:create
    bundle exec rake db:migrate
    

    If the above command doesn't please down this migration 20231003192318_add_timestamps_to_articles. Please use

    bundle exec rake db:migrate:down VERSION=20231003192318
    

    Note: You need to have down method on that migration file to perform down migration. Ref: Migration Up/Down vs Change