【问题标题】:Rails 4 set the default value of a existing column to zeroRails 4 将现有列的默认值设置为零
【发布时间】:2014-06-01 18:57:07
【问题描述】:

这个问题无法解释更多。

db/migrate/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"

【问题讨论】:

  • 你说的“灭柱”是什么意思?
  • 对不起,我的意思是现有的

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


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2013-11-11
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2019-08-17
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多