0

i have migrtion:

def change
    create_table :m_roles do |t|
      t.string :name, null: false, index: { unique: true }, default: ""

      t.timestamps
    end
  end

schema:

create_table "m_roles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "name", default: "", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["name"], name: "index_m_groups_on_name", unique: true
  end

I would like to change it to the following:

null: false -> null: true and remove default: ""

But I don't know what to write about the new migration: i try but it not rollback:

Gautam
  • 1,754
  • 1
  • 14
  • 22
duong tang
  • 31
  • 3
  • 1
    What have you tried? What do you mean by *"It not rollback"*? – Tom Lord Jan 03 '20 at 09:44
  • You don't necessarily need to rollback the migration. If you haven't yet deployed this code to production, then you *could* just rollback and delete it... Or, regardless of whether this is on production, you could write a second migration to change the column null and default value. – Tom Lord Jan 03 '20 at 09:45
  • Try doing `change_column :m_roles, :name, :string, null: true`. To achieve you need to create a migration file and run this migration. – Santosh Aryal Jan 03 '20 at 09:45
  • You can find information about this on places like SO, or directly in the rails documentation. For example, see: https://stackoverflow.com/a/22994790/1954610 and https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_column_default – Tom Lord Jan 03 '20 at 09:47
  • yes ```change_column :m_roles, :name, :string, null: true``` it want add option – duong tang Jan 03 '20 at 09:47
  • my migration : ```change_column :m_roles, :name, :string, null :true, form: "", to: nil``` – duong tang Jan 03 '20 at 09:48
  • What do you mean you want to add options? – Santosh Aryal Jan 03 '20 at 09:48
  • but ```default ""``` What do I do to make it disappear? – duong tang Jan 03 '20 at 09:48
  • I don't think this will affect your application, since it field will be nil by default – Santosh Aryal Jan 03 '20 at 09:50
  • more: It forces "up" and "down" – duong tang Jan 03 '20 at 09:56

1 Answers1

0

If you already have data in your database, with a name of "" and you need to manually update it, you could manually specify up and down and do a AR query to update the existing columns:

def up
    change_column :m_roles, :name, null: true, default: nil
    MRoles.where(name: '').update_all(name: nil)
end

def down
    MRoles.where(name: nil).update_all(name: '')
    change_column :m_roles, :name, null: false, default: ''
end

Something to be aware of is the change_column_null method, but because you've set the value to be blank I'm not sure that would work, but would look something like:

def change
                  #   table    column   allow nil?, default
  change_column_null :m_roles,  :name,   true,       ''
end
Yule
  • 9,668
  • 3
  • 51
  • 72