Skip to content
Browse files

Handle nil in add_index :length option in MySQL

Our schema.rb is being generated with an `add_index` line similar to this:

    add_index "foo", ["foo", "bar"], :name => "xxx", :length => {"foo"=>8, "bar=>nil}

This is the same as it was on Rails 3.1.3, however, now when that
schema.rb is evaluated, its generating bad SQL in MySQL:

    Mysql::Error: You have an error in your SQL syntax; check the manual
    that corresponds to your MySQL server version for the right syntax
    to use near '))' at line 1: CREATE UNIQUE INDEX
    `xxx` ON `foo` (`foo`(8), `bar`())

This commit adds a check for nil on the length attribute to prevent the
empty parens from being output.



Signed-off-by: José Valim <>
  • Loading branch information...
1 parent 77b4edc commit 0a75336c89a517bf2328a6e5f87319b9f2457252 @paul paul committed with josevalim Jan 24, 2012
2 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -541,7 +541,7 @@ def add_index_length(option_strings, column_names, options = {})
if options.is_a?(Hash) && length = options[:length]
case length
when Hash
- column_names.each {|name| option_strings[name] += "(#{length[name]})" if length.has_key?(name)}
+ column_names.each {|name| option_strings[name] += "(#{length[name]})" if length.has_key?(name) && length[name].present?}
when Fixnum
column_names.each {|name| option_strings[name] += "(#{length})"}
5 activerecord/test/cases/migration_test.rb
@@ -161,6 +161,11 @@ def test_add_index_length_limit
Person.connection.remove_index("people", :name => good_index_name)
+ def test_add_index_attribute_length_limit
+ connection.add_index :testings, [:foo, :bar], :length => {:foo => 10, :bar => nil}
+ assert connection.index_exists?(:testings, [:foo, :bar])
+ end
def test_remove_nonexistent_index
# we do this by name, so OpenBase is a wash as noted above
unless current_adapter?(:OpenBaseAdapter)

0 comments on commit 0a75336

Please sign in to comment.
Something went wrong with that request. Please try again.