rename_column迁移期间生成的异常

时间:2010-10-13 02:46:22

标签: ruby-on-rails rake

我正在尝试通过迁移更改我的sqlite数据库中的列...而且我不知道我在使用RoR做什么,所以请保持温和!

这是我正在尝试运行的迁移。

class FixContactIdColumn < ActiveRecord::Migration
  def self.up
    rename_column :calls, :contact, :contact_id
  end

  def self.down
  end
end

这是它产生的例外。

brad@brad-vm:~/www/contactly$ vim db/migrate/20101013020151_fix_contact_id_column.rb
brad@brad-vm:~/www/contactly$ rake db:migrate --trace
(in /home/brad/www/contactly)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
==  FixContactIdColumn: migrating =============================================
-- rename_column(:calls, :contact, :contact_id)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `to_sym' for nil:NilClass
/usr/lib/ruby/1.8/active_support/whiny_nil.rb:52:in `method_missing'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/schema_definitions.rb:480:in `column'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:363:in `copy_table'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:357:in `each'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:357:in `copy_table'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/schema_statements.rb:104:in `create_table'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:355:in `copy_table'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:349:in `move_table'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:342:in `alter_table'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:341:in `alter_table'
/usr/lib/ruby/1.8/active_record/connection_adapters/sqlite_adapter.rb:311:in `rename_column'
/usr/lib/ruby/1.8/active_record/migration.rb:352:in `send'
/usr/lib/ruby/1.8/active_record/migration.rb:352:in `method_missing'
/usr/lib/ruby/1.8/active_record/migration.rb:328:in `say_with_time'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/lib/ruby/1.8/active_record/migration.rb:328:in `say_with_time'
/usr/lib/ruby/1.8/active_record/migration.rb:348:in `method_missing'
./db/migrate//20101013020151_fix_contact_id_column.rb:3:in `up_without_benchmarks'
/usr/lib/ruby/1.8/active_record/migration.rb:282:in `send'
/usr/lib/ruby/1.8/active_record/migration.rb:282:in `migrate'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/lib/ruby/1.8/active_record/migration.rb:282:in `migrate'
/usr/lib/ruby/1.8/active_record/migration.rb:365:in `__send__'
/usr/lib/ruby/1.8/active_record/migration.rb:365:in `migrate'
/usr/lib/ruby/1.8/active_record/migration.rb:486:in `migrate'
/usr/lib/ruby/1.8/active_record/migration.rb:560:in `call'
/usr/lib/ruby/1.8/active_record/migration.rb:560:in `ddl_transaction'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
/usr/lib/ruby/1.8/active_record/transactions.rb:182:in `transaction'
/usr/lib/ruby/1.8/active_record/migration.rb:560:in `ddl_transaction'
/usr/lib/ruby/1.8/active_record/migration.rb:485:in `migrate'
/usr/lib/ruby/1.8/active_record/migration.rb:472:in `each'
/usr/lib/ruby/1.8/active_record/migration.rb:472:in `migrate'
/usr/lib/ruby/1.8/active_record/migration.rb:400:in `up'
/usr/lib/ruby/1.8/active_record/migration.rb:383:in `migrate'
/home/brad/www/contactly/vendor/rails/railties/lib/tasks/databases.rake:116
/usr/lib/ruby/1.8/rake.rb:636:in `call'
/usr/lib/ruby/1.8/rake.rb:636:in `execute
/usr/lib/ruby/1.8/rake.rb:631:in `each'
/usr/lib/ruby/1.8/rake.rb:631:in `execute'
/usr/lib/ruby/1.8/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:583:in `invoke'
/usr/lib/ruby/1.8/rake.rb:2051:in `invoke_task'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2029:in `each'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:2023:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2001:in `run'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:1998:in `run'
/usr/bin/rake:28

有什么想法吗?我很乐意提供更多细节......

更新:有人建议我尝试回滚以前的迁移...但是我得到了类似的结果。

-rw-r--r-- 1 brad brad  166 2010-10-12 17:21 20101013002111_add_notes_to_call.rb
-rw-r--r-- 1 brad brad  175 2010-10-12 17:22 20101013002240_add_contact_to_call.rb
-rw-r--r-- 1 brad brad  146 2010-10-12 19:25 20101013020151_fix_contact_id_column.rb
brad@brad-vm:~/www/contactly/db/migrate$ rake db:migrate VERSION=20101013002111
(in /home/brad/www/contactly)
==  AddContactToCall: reverting ===============================================
-- remove_column(:calls, :contact)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `to_sym' for nil:NilClass

(See full trace by running task with --trace)

2 个答案:

答案 0 :(得分:1)

虽然应该在schema_definitions.rb中解决此问题,但您可以通过为to_sym创建NilClass实例方法来消除此错误。

config/initializers/nil_class.rb下的rails应用程序中创建初始化程序并将其添加到其中:

class NilClass
  def to_sym
    nil
  end
end

完成此操作后,请尝试重新运行迁移。

答案 1 :(得分:0)

请发布原始迁移。

如果您的迁移尚未在制作中,只需编辑原始迁移以满足您的需要并运行:

rake db:migrate VERSION=<migration_date>
rake db:migrate 

migration_date是迁移文件名称中的时间戳。还要记住,您必须在计划重新执行之前选择迁移,因此如果您有:

201010101005_create_users.rb
201010101010_create_calls.rb

你想跑:

rake db:migrate VERSION=201010101005