Rails 4将现有列的默认值设置为零

时间:2014-04-17 13:16:51

标签: ruby-on-rails sqlite ruby-on-rails-4

这个问题无法解释得更多。

分贝/迁移/ 20140415150026_create_poll_answers.rb

class CreatePollAnswers < ActiveRecord::Migration
  def change
    create_table :poll_answers do |t|
      t.string :content
      t.integer :counter 
      t.boolean :instant_value
      t.timestamps
    end
  end
end

分贝/模式

create_table "poll_answers", force: true do |t|
  t.string   "content"
  t.integer  "counter"
  t.boolean  "instant_value"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "poll_question_id"
end 

我找到了答案,但我不确定它适用于rails 4,我也不知道应该写在哪里!!!

add_column :Table_name, :colman_name, :data_type, :default => "default"

1 个答案:

答案 0 :(得分:3)

您可以为新迁移设置这样的默认值:

create_table :poll_answers, force: true do |t|
  t.string  :content, default: 'no_text'
  t.integer :counter, default: 0
  t.float   :money_in_pocket, default: 0.0 
 end

或者您可以更改此类现有迁移:

def change
  change_column :poll_answers, :counter, :integer, default: 100
end

甚至更短:

change_column_default(:poll_answers, :counter, 3000)