In a Rails Migration, specifying a column as binary generates a blob column. Blob columns, unfortunately, are allocated differently than any other MySQL column type; specifically, they aren’t supported in memory engine-backed tables.

Varbinary columns, on the other hand, are supported. In order to provide varbinary columns in a Migration, I wrote up this simple core extension.

# Specify :varbinary => true in column creation
# No support yet for altering tables to varbinary
class ActiveRecord::ConnectionAdapters::MysqlAdapter < AbstractAdapter
  def type_to_sql_with_varbinary(type, limit = nil, precision = nil, scale = nil)
    return type_to_sql_without_varbinary(type, limit, precision, scale) unless :varbinary == type.to_sym
    "varbinary(#{limit})"
  end
  alias_method_chain :type_to_sql, :varbinary
end
 
class ActiveRecord::ConnectionAdapters::TableDefinition
  def column_with_varbinary name, type, options = {}
    return column_without_varbinary name, "varbinary", options if options.delete(:varbinary)
    column_without_varbinary name, type, options
  end
  alias_method_chain :column, :varbinary
end

One Comment

  1. Alexis

    In ActiveRecord 3.2.21, you can overwrite blob defined in lib/active_record/connection_adapters/abstract_mysql_adapter.rb by varbinary:

    ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES[:binary][:name] = “varbinary”

Leave a Comment

Enclose code in <code lang="ruby"></code> if you care.
Preview your comment using the button below.