0

I have a table which was originally defined with a default value for one column. I need to change or remove the default.

  • RubyOnRails.org seems (by its silence) to say this change is not wrapped for portability by ActiveRecord::Migration.
  • W3schools seems to say the SQL needed is not portable between MySQL and SQLite (my two DBs).

If I misread either of those, then straightening me out would be sufficient. Otherwise:

  • From within an ActiveRecord::Migration#up routine, how can I write an execute call that's sensitive to the underlying DB? Can I, for instance, see the active values from config/database.yml?

Current table defn (from db/schema.rb) is:

  create_table "apis", force: true do |t|
    t.string   "provider"
    t.string   "endpoint"
    t.string   "name"
    t.integer  "owner_id"
    t.string   "status"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "apiary_dev_domain"
    t.string   "resource_root",     default: "/v1", null: false
    t.text     "description"
  end
jackr
  • 1,407
  • 1
  • 14
  • 29
  • Which are the specifications of your column? – VMai Aug 28 '14 at 22:28
  • possible duplicate of [Adding :default => true to boolean in existing Rails column](http://stackoverflow.com/questions/8627156/adding-default-true-to-boolean-in-existing-rails-column) – Brad Werth Aug 29 '14 at 06:28

1 Answers1

0

Thanks, Brad Werth:; you were right, this is effectively a dupe of Adding default => true to boolean in existing Rails column.

ActiveRecord::Migration#change_column did the trick. Specifically, my migration script is:

class DeslashifyUrlComponentsInApi < ActiveRecord::Migration
  def up
    change_column :apis, :resource_root, :string, default: "v1", null: false
  end
  def down
    change_column :apis, :resource_root, :string, default: "/v1", null: false
  end
end
Community
  • 1
  • 1
jackr
  • 1,407
  • 1
  • 14
  • 29