In my Rails app, I have a model called Rutinas. After some time, I needed to add some columns to the table so I generated a migration 20171116094810_add_votos_y_veces_asignada_to_rutinas.rb
:
class AddVotosYVecesAsignadaToRutinas < ActiveRecord::Migration[5.1]
def change
add_column :rutinas, :votos_pos, :integer, :default => 0
add_column :rutinas, :votos_neg, :integer, :default => 0
add_column :rutinas, :veces_asig, :integer, :default => 0
end
end
After some other migrations, I needed to delete two columns that I don't need anymore votos_pos
and votos_neg
so I generated another migration 20171117092026_remove_votos_from_rutinas.rb
:
class RemoveVotosFromRutinas < ActiveRecord::Migration[5.1]
def change
remove_column :rutinas, :votos_pos, :integer
remove_column :rutinas, :votos_neg, :integer
end
end
The problem is that when I run rails db:migrate
to migrate this last migration, it throws some weird error:
== 20171117092026 RemoveVotosFromRutinas: migrating ===========================
-- remove_column(:rutinas, :votos_pos)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "rutinas"
C:/Users/pepe/Dropbox/pepe/KeepMeFit/KeepMeFit-git/db/migrate/20171117092026_remove_votos_from_rutinas.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
ActiveRecord::InvalidForeignKey: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "rutinas"
C:/Users/pepe/Dropbox/pepe/KeepMeFit/KeepMeFit-git/db/migrate/20171117092026_remove_votos_from_rutinas.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
SQLite3::ConstraintException: FOREIGN KEY constraint failed
C:/Users/pepe/Dropbox/pepe/KeepMeFit/KeepMeFit-git/db/migrate/20171117092026_remove_votos_from_rutinas.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
My Rutinas model is the following:
class Rutina < ActiveRecord::Base
validates_presence_of :nombre
belongs_to :user
has_many :repeticions
has_many :entrenos, dependent: :destroy
has_many :votos
has_many :days, dependent: :destroy
def get_array_dias
(1..self.repeticions.last.dia).to_a
end
def get_number_of_days
days.count
end
end
User, repeticion, entreno, voto and day are other tables which are non-related to the columns that I'm trying to delete. That is to say, these columns are not a foreign key and any foreign key references these columns.