how to resolve pending migration error in rails 7

374 views Asked by At

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

3

There are 3 answers

9
Cattani On

If you are using create_table, e.g.

create_table :saving_transactions do |t|
  t.integer :value_in_cents

  t.timestamps
end

t.timestamps will create the created_at and updated_at fields for you, so leave it there but remove the extra created_at you added manually.

When you do, you have two choices:

  1. If you don't care about losing the data you have you can use rails db:reset. This will drop the databases and run the migrations again (so make sure to fix the issue of having an extra created_ at before running the command)

  2. If you don't want to lose the data you can use rails db:rollback. It will rollback the last migration and you can pass how many steps you want (in your case is 1, but you don't need to pass it) with STEP. After that just run rails db:migrate again.

For example:

rails db:rollback STEP=3 # => Rollsback 3 migrations

More infos here: https://guides.rubyonrails.org/active_record_migrations.html#rolling-back

1
Tallboy On

You didnt show your migration file, but if you see the line t.timestamps in your original migration, then created_at is already created along with updated_at.

If you don't want BOTH created_at and updated_at, then delete the line and explicitly define a single one of them, for example t.datetime :created_at

After you change your migration, do the following (will wipe your local data):

rake db:drop
rake db:create
rake db:migrate

Locally you do not need to worry about rolling back or all that stuff. Just wipe the entire DB and keep starting fresh. Locally this is fine because thats the point of seed data, is to keep re-seeding your app and you wipe and clear the database each time you change the migrations. Theres no reason to keep adding migration files when the app isn't launched yet

if you are encountering this issue in production, then IMO i always just hand edit the database by hand because I hate hundreds of unnecessary migrations.

So in production I would basically make a commit locally of the migration changes I want, and mentally note them. Then open my production database in a GUI and modify them as I see fit, making them match the way the database looks locally in the same GUI. This is good for smaller apps that dont need a complicated audit log of every change.

0
Sriram On

You have created new migration but not yet migrated to your DB, so just use

bundle exec rails db:migrate

If the above command doesn't works then use rake db:migrate