删除列 Rails 时出错 [英] Error removing column Rails

查看:47
本文介绍了删除列 Rails 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用程序中,我有一个名为 Rutinas 的模型.一段时间后,我需要向表中添加一些列,因此我生成了迁移 20171116094810_add_votos_y_veces_asignada_to_rutinas.rb:

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

在其他一些迁移之后,我需要删除不再需要的两列 votos_posvotos_neg 所以我生成了另一个迁移 20171117092026_remove_votos_from_rutinas.rb:

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

问题是当我运行 rails db:migrate 来迁移最后一次迁移时,它抛出了一些奇怪的错误:

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)

我的 Rutinas 模型如下:

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

用户、重复、entreno、voto 和 day 是其他与我尝试删除的列无关的表.也就是说,这些列不是外键,任何外键都引用这些列.

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.

推荐答案

它将在 Rails 6 中修复,包括 https://github.com/rails/rails/pull/32865.这需要 SQLite 数据库 3.8 及更高版本.我认为它不会重新移植到 Rails 5.2 或更早版本中.

It will be fixed in Rails 6 including https://github.com/rails/rails/pull/32865 . This requires SQLite database 3.8 and higher. I do not think it will be back ported into Rails 5.2 or older.

我的回答可能不会给你任何关于如何编写代码的实际反馈,我现在可以说的是升级 Rails 6(发布日期不固定)可能会解决这个问题.

My answer may not give you any practical feedback how to write your code, what I can say now is upgrading Rails 6 (release date is not fixed) may resolve this problem.

这篇关于删除列 Rails 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆