Rails 数据库模型未更新

Rails database model not updating

我正在为名为 SimutronX 的优化机器创建数据库模型。我刚刚更改了其中一个数据库键的变量名并添加了一个索引。

当前迁移文件:

class CreateSimutronXes < ActiveRecord::Migration[5.0]
  def change
    create_table :simutron_xes do |t|
      t.integer :num_constraints
      t.integer :num_coefficients
      t.boolean :max
      t.decimal :obj_func

      t.timestamps
    end
    add_index :simutron_xes, [:user_id, :created_at]
  end
end

旧迁移文件:

class CreateSimutronXes < ActiveRecord::Migration[5.0]
  def change
    create_table :simutron_xes do |t|
      t.integer :num_constraints
      t.integer :num_coefficients
      t.boolean :min_max
      t.decimal :obj_func

      t.timestamps
    end
  end
end

我已经保存了新文件并且 运行 一个 rails db:migrate 已成功完成。 运行 迁移后,我进入 rails 控制台沙箱来测试模型。

brotherlongtail:~/workspace/Simutronix (master) $ rails console --sandbox
Running via Spring preloader in process 8135
Loading development environment in sandbox (Rails 5.0.2)
Any modifications you make will be rolled back on exit
>> SimutronX.new
=> #<SimutronX id: nil, constraint: nil, coefficient: nil, min_max: nil, obj_func: nil, created_at: nil, updated_at: nil>

模型好像没有更新。让模型正确更新需要哪些额外步骤?

运行之后改变原来的迁移没有效果。您将不得不使用 rake db:rollback 回滚到该特定迁移之前的版本。然而,我发现在开发时从数据库中删除 table 并创建一个新的迁移和 运行 更容易。

Rails 跟踪 运行 在 table schema_migrations 中的迁移,因此一种解决方案也是删除与您想要的迁移对应的行改变。然而,这不是一个非常优雅的解决方案。