【问题标题】:MIgration didn't work rails迁移无法正常工作
【发布时间】:2016-12-18 15:12:51
【问题描述】:

我在tasks 表中有一个属性,它被命名为is_active,它是boolean。但是我在创建表时忘记为这个属性设置默认值。所以现在我尝试将此值默认设置为true,但正如我从sqlite浏览器中看到的那样 - 新任务仍在创建,此值设置为NULL。您能帮我找出迁移中的问题吗?

迁移文件:

class AddDefaultValueToTask < ActiveRecord::Migration[5.0]
  def change
    def up
      change_column :tasks, :is_active, :boolean, :default =>  true
    end

    def down
      change_column :tasks, :is_active, :boolean, :default =>  nil
    end
  end
end

Schema.rb 文件

create_table "tasks", force: :cascade do |t|
    t.text     "body"
    t.boolean  "is_active"
    t.integer  "project_id"
    t.string   "deadline"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["project_id"], name: "index_tasks_on_project_id"
  end

【问题讨论】:

    标签: sql ruby-on-rails sqlite


    【解决方案1】:

    您缺少哈希火箭运算符:'=&gt;'

    change_column :tasks, :is_active, :boolean, :default => true
    

    另外,如果您使用的是updown,那么您需要删除change 块。两种方法不能同时使用

    【讨论】:

    • 逆向迁移,添加 hashrocket,仍然没有变化
    • 尝试取出def change 块,并留下updown 块。看看有没有帮助
    猜你喜欢
    • 2017-10-05
    • 2020-03-15
    • 2018-07-21
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多