This project is archived and is in readonly mode.

#3234 open
Greg Hazel

change_column carries over default when it can't be carried

Reported by Greg Hazel | September 19th, 2009 @ 02:34 PM | in 2.3.10

related to: http://dev.rubyonrails.org/ticket/10963

add_column :mytable, :data, :string
change_column :mytable, :data, :binary

Will fail (on MySQL anyway) with:

Mysql::Error: BLOB/TEXT column 'data' can't have a default value: ALTER TABLE `mytable` CHANGE `data` `data` blob DEFAULT '' NOT NULL

This is because the default from the string column, which defaults to '', is carried over to the new column.

Culprit:

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  column = column_for(table_name, column_name)

  unless options_include_default?(options)
    options[:default] = column.default
  end

  unless options.has_key?(:null)
    options[:null] = column.null
  end

See how it takes column.default without considering the type of the new column?

Similar to has_default?

def has_default?
  return false if type == :binary || type == :text #mysql forbids defaults on blob and text columns
  super
end

It could check the type. Something like:

-  unless options_include_default?(options)
+  unless options_include_default?(options) or type == :binary or type == :text #mysql forbids defaults on blob and text
     options[:default] = column.default
   end

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages