SchemaPlus / schema_plus

SchemaPlus provides a collection of enhancements and extensions to ActiveRecord
Other
679 stars 84 forks source link

change_column uses default foreign key configuration instead of defined configuration causing migration to crash #213

Closed robinson-spai closed 9 years ago

robinson-spai commented 9 years ago

I'm having a similar issue. I have a migration changing a string column to a text column as follows:

class ChangeCommentOfPunchlistHistoryToText < ActiveRecord::Migration
  def change
    change_column :form_extensions_punchlist_form_histories, :comment, :text
  end
end

FormExtensions::PunchlistFormHistory belongs to Forms::PunchlistForm with a column punchlist_form_id that explicitly has no foreign key constraint.

However, when the change migration is run, temporary tables are created in the background with foreign key constraints causing errors because the default foreign key naming exceeds the 62 character limit of sqlite. Why are the default names being used in the background?

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Index name 'fk__aform_extensions_punchlist_form_histories_punchlist_form_id' on table 'aform_extensions_punchlist_form_histories' is too long; the limit is 62 characters/Users/sp
ai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:797:in `add_index_options'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:536:in `add_index'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/schema_plus-1.8.8/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:94:in `add_index_with_schema_plus'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:206:in `block in create_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:206:in `each_pair'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:206:in `create_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/schema_plus-1.8.8/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:39:in `create_table_with_schema_plus'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in `copy_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:531:in `move_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:524:in `block in alter_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:199:in `transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:523:in `alter_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:487:in `change_column'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/schema_plus-1.8.8/lib/schema_plus/active_record/foreign_keys.rb:132:in `change_column'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:656:in `block in method_missing'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:628:in `block in say_with_time'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:628:in `say_with_time'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:648:in `method_missing'
/Users/spai/Documents/Work/Eclipse/FormData/form-data/db/migrate/20150129194706_change_comment_of_punchlist_history_to_text.rb:3:in `change'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:602:in `exec_migration'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:586:in `block (2 levels) in migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:585:in `block in migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:584:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:759:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/transactions.rb:208:in `transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:1044:in `ddl_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:959:in `block in migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:955:in `each'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:955:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:814:in `up'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:792:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
ArgumentError: Index name 'fk__aform_extensions_punchlist_form_histories_punchlist_form_id' on table 'aform_extensions_punchlist_form_histories' is too long; the limit is 62 char
acters
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:797:in `add_index_options'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:536:in `add_index'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/schema_plus-1.8.8/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:94:in `add_index_with_schema_plus'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:206:in `block in create_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:206:in `each_pair'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/schema_statements.rb:206:in `create_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/schema_plus-1.8.8/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:39:in `create_table_with_schema_plus'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in `copy_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:531:in `move_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:524:in `block in alter_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:199:in `transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:523:in `alter_table'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/sqlite3_adapter.rb:487:in `change_column'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/schema_plus-1.8.8/lib/schema_plus/active_record/foreign_keys.rb:132:in `change_column'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:656:in `block in method_missing'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:628:in `block in say_with_time'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:628:in `say_with_time'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:648:in `method_missing'
/Users/spai/Documents/Work/Eclipse/FormData/form-data/db/migrate/20150129194706_change_comment_of_punchlist_history_to_text.rb:3:in `change'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:602:in `exec_migration'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:586:in `block (2 levels) in migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:585:in `block in migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:584:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:759:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/transactions.rb:208:in `transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:1044:in `ddl_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:959:in `block in migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:955:in `each'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:955:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:814:in `up'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/migration.rb:792:in `migrate'
/Users/spai/.rvm/gems/ruby-2.1.3/gems/activerecord-4.1.10/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
ronen commented 9 years ago

@robinson-spai thanks for the bug report. I've released version 1.8.9 which should fix this for you. Let me know if you have other problems.