This project is archived and is in readonly mode.
Rails 3.0.0 add_index fails silently on invalid index names
Reported by Tim Connor | September 17th, 2010 @ 04:08 AM
If add_index is called with a name that is too long, the migration appears to be successful but the index is not created.
The error is very easy to reproduce. Create a new rails project, then run the following migration. After execution the table will be created but the index will not.
class TestOfAddIndex < ActiveRecord::Migration
def self.up
create_table :blah do |t|
t.string :name
end
add_index :blah, [:name], :name => "a really long name that is too long for the database to handle and should cause an error"
end
def self.down
end
end
The output of the migration is:
$ rake db:migrate
(in /Volumes/projects/rails/add_index_test)
== TestOfAddIndex: migrating =================================================
-- create_table(:blah)
-> 0.0016s
-- add_index(:blah, [:name], {:name=>"a really long name that is too long for the database to handle and should cause an error"})
-> 0.0002s
== TestOfAddIndex: migrated (0.0020s) ========================================
And the resulting schema.rb is:
ActiveRecord::Schema.define(:version => 20100917024939) do
create_table "blah", :force => true do |t|
t.string "name"
end
end
I have attached a patch that will fix the problem.
Comments and changes to this ticket
-
Tim Connor September 17th, 2010 @ 04:14 AM
This issue also applies to index names automatically generated by rails, eg
add_index :users, [:long_column_name, :other_long_column_name, :third_long_column_name], :unique => true
will appear to work but the index will not be created as the generated name was too long
-
Lake September 17th, 2010 @ 04:48 AM
Thanks for you contribution.
Unfortunately, your patch actually breaks a couple of tests. I recommend that you check out those tests and try to resolve them.
1) Failure: test_add_index_length_limit(MigrationTest) [test/cases/migration_test.rb:130]: Exception raised: <#<ArgumentError: Index name 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' on table 'people' is too long; the limit is 64 characters>>. 2) Failure: test_double_add_index(MigrationTest) [test/cases/migration_test.rb:157]: Exception raised: <#<ArgumentError: Index name 'some_idx' on table 'people' already exists>>. 126 tests, 343 assertions, 2 failures, 0 errors, 0 skips
-
Tim Connor September 17th, 2010 @ 05:38 AM
Attached is a second patch which fixes a further issue with remove_index and updates the unit tests.
-
Rohit Arondekar September 18th, 2010 @ 02:49 AM
- State changed from new to open
- Assigned user set to José Valim
- Importance changed from to Low
-
Repository September 18th, 2010 @ 07:50 PM
- State changed from open to resolved
(from [37de59eacf8f6478e866309615af7381d41a5a14]) Fix issue with remove_index and add unit test [#5645 state:resolved] http://github.com/rails/rails/commit/37de59eacf8f6478e866309615af73...
-
Repository September 27th, 2010 @ 02:00 PM
(from [d3ffea25b2b762ff7af5172e7d501684a8b1ef96]) Fix issue with remove_index and add unit test [#5645 state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/d3ffea25b2b762ff7af5172e7d5016...
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>
People watching this ticket
Attachments
Referenced by
- 5645 Rails 3.0.0 add_index fails silently on invalid index names (from [37de59eacf8f6478e866309615af7381d41a5a14]) Fix iss...
- 5645 Rails 3.0.0 add_index fails silently on invalid index names (from [d3ffea25b2b762ff7af5172e7d501684a8b1ef96]) Fix iss...