作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我运行了以下迁移,却忘了包含我想要的默认值。我正在尝试将其回滚,以便可以添加默认值并再次将其前滚。
20190728151635_add_cooldown_to_skill_levels.rb
class AddCooldownToSkillLevels < ActiveRecord::Migration[5.1]
def change
add_column :skill_levels, :cooldown, :integer
end
end
== 20190728151635 AddCooldownToSkillLevels: reverting =========================
-- remove_column(:skill_levels, :cooldown, :integer)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "skill_levels"
And 100+ more lines ...
class AddCooldownToSkillLevels < ActiveRecord::Migration[5.1]
def up
add_column :skill_levels, :cooldown, :integer
end
def down
remove_column :skill_levels, :cooldown
end
end
class AddDefaultValueToCooldownOnSkillLevels < ActiveRecord::Migration[5.1]
def change
change_column_default :skill_levels, :cooldown, 1
end
end
== 20190728153208 AddDefaultValueToCooldownOnSkillLevels: migrating ===========
-- change_column_default(:skill_levels, :cooldown, 1)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "skill_levels"
And 100+ more lines ...
...
up 20190727160901 Create skill levels
up 20190728004535 Create skill effects
up 20190728151635 Add cooldown to skill levels
down 20190728153208 Add default value to cooldown on skill levels
最佳答案
错误:
FOREIGN KEY constraint failed: DROP TABLE "skill_levels"
skill_levels
表(即其他迁移中的
t.references :skill_levels, foreign_key: true
)的外键约束,而
remove_column :skill_levels, :cooldown
导致此问题。
up
和
down
方法,然后将
down
方法修改为:
remove_foreign_key
。
remove_column :skill_levels, :cooldown
。
add_foreign_key
。
def down
remove_foreign_key :some_table, :skill_levels
remove_column :skill_levels, :cooldown
add_foreign_key :some_table, :skill_levels
end
关于ruby-on-rails - 如何回滚Rails迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57242929/
我是一名优秀的程序员,十分优秀!