删除列导轨时出错



在我的Rails应用程序中,我有一个名为Rutinas的模型。一段时间后,我需要向表中添加一些列,因此我生成了一个迁移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

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来迁移最后一个迁移时,它会抛出一些奇怪的错误:

== 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模型如下:

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是与我尝试删除的列无关的其他表。也就是说,这些列不是外键,任何外键都引用这些列。

似乎问题在于SQLite的限制。如果您查看文档,您将看到它只允许添加列,但不允许删除列:https://sqlite.org/lang_altertable.html

迁移

实际上可能会删除整个表,并在删除列时重新创建它。这将解释DROP TABLE "rutinas"消息。当然,如果删除 while 表,则某些外键约束失败是有意义的。

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

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

相关内容

  • 没有找到相关文章

最新更新