acts_as_versioned icon indicating copy to clipboard operation
acts_as_versioned copied to clipboard

MySQL/PosgreSQL: Index name on table is too long; the limit is 63 characters

Open cure opened this issue 13 years ago • 0 comments

Rails >= 3.0.3 aborts migrations if they try to create an index with a name are longer than 64 characters on PostgreSQL or MySQL (yes, 64, not 63 like the error says).

When acts_as_versioned creates indexes, it does not check the length of the resulting index name.

The patch below fixes that. It fixes the problem on MySQL and PostgreSQL (I tested both).

--- acts_as_versioned.rb    2011-05-06 11:37:30.000000000 -0400
+++ acts_as_versioned.rb    2011-05-06 11:29:47.000000000 -0400
@@ -440,7 +440,9 @@
                                          :precision => type_col.precision
             end
 
-            self.connection.add_index versioned_table_name, versioned_foreign_key
+            # Make sure not to create an index that is too long (postgres limits index names to 64 characters)
+            name = 'index_' + versioned_table_name + '_on_' + versioned_foreign_key
+            self.connection.add_index versioned_table_name, versioned_foreign_key, :name => name[0,63]
           end
 
           # Rake migration task to drop the versioned table

cure avatar May 06 '11 15:05 cure