【问题标题】:I'm having trouble dropping a migration during a db:reset. Are my migrations incorrect?我在 db:reset 期间无法删除迁移。我的迁移不正确吗?
【发布时间】:2014-09-04 20:20:02
【问题描述】:

我在运行 db:reset 时遇到此错误

rake aborted!
ActiveRecord::StatementInvalid: PG::DependentObjectsStillExist: ERROR:  cannot drop table seasons because other objects depend on it
DETAIL:  constraint weeks_season_id_fk on table weeks depends on table seasons
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
: DROP TABLE "seasons"

我添加的最新迁移是...

class AddSeasonIdToWeek < ActiveRecord::Migration
  def change
    add_column :weeks, :season_id, :integer, null: false, index: true
  end
end

class AddForeignKeySeasonsToWeeks < ActiveRecord::Migration
  def change
    change_table :weeks do |a|
      a.foreign_key :seasons
    end
  end
end

我需要做什么才能克服这个错误?

【问题讨论】:

  • 您是否使用外国人 gem 来允许外键迁移?

标签: ruby-on-rails migration rake


【解决方案1】:

尝试在 rails 4 中使用它:

rake db:rollback

两次

在旧版本的 Rails 中(可能是 3 和更早版本)

rake db:migrate:down

两次

【讨论】:

  • 另一种选择是只使用sql手动更新子记录中的外键为null,然后您可以删除父记录。
  • 好吧,您可以改为将dependent: :delete 添加到关系中,这将在删除时使外键无效
  • 当你说“两次”时,你的意思是连续两次运行命令吗?
猜你喜欢
  • 1970-01-01
  • 2022-11-19
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 2022-12-30
  • 1970-01-01
相关资源
最近更新 更多