Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my rails db:migrate not working?

I typed in rails db:migrate and received the following error. I googled and someone said something like changing the Migration version from [4.2] to [5.1] but it still has not worked.

rails db:migrate rails aborted! StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

class CreateCkeditorAssets < ActiveRecord::Migration[4.2]

like image 393
jose tanaka Avatar asked Jun 07 '17 13:06

jose tanaka


People also ask

How Rails db Migrate works?

When you run db:migrate, rails will check a special table in the database which contains the timestamp of the last migration applied to the database. It will then apply all of the migrations with timestamps after that date and update the database table with the timestamp of the last migration.

How can I check my Rails migration status?

rake db:migrate:status (Rails 3 to 5) or rails db:migrate:status (Rails 5) will accomplish this. See this commit. up means the migration has been run. down means the migration has not been run.


2 Answers

Aditya already wrote the answer, Changing all migrations by hand is a hectic task, So I wrote a one liner script to do that

In linux (Gnu sed)

grep -rl "ActiveRecord::Migration$" db | xargs sed -i 's/ActiveRecord::Migration/ActiveRecord::Migration[4.2]/g' 

In Mac (BSD sed)

grep -rl "ActiveRecord::Migration$" db | xargs sed -i "" "s/ActiveRecord::Migration/ActiveRecord::Migration[4.2]/g" 

Note, you can replace 4.2 with the rails from which you are upgrading to 5.1

like image 53
Tachyons Avatar answered Sep 21 '22 13:09

Tachyons


Rails 5 changed the way migrations are created. You'll have to specify the Rails release starting Rails 5 like this (assuming you're using Rails 5.1):

class CreateCkeditorAssets < ActiveRecord::Migration[5.1] 

Alternately, you can try creating a test migration and see how your version of Rails generates a migration and then take it from there:

rails g model Test name:string 
like image 35
Aditya Avatar answered Sep 17 '22 13:09

Aditya