2010-10-13 69 views
3

我想通過遷移來更改我的sqlite數據庫中的列......並且我不知道我在用RoR做什麼,所以請溫柔!在rename_column遷移期間生成的異常

這是我試圖運行的遷移。

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

    def self.down 
    end 
end 

這是它生成的異常。

[email protected]:~/www/contactly$ vim db/migrate/20101013020151_fix_contact_id_column.rb 
[email protected]:~/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 
[email protected]:~/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) 
+0

有沒有可能'調用'表沒有'contact'列?因爲這在我看來是如此。 – nathanvda 2010-10-13 10:47:20

回答

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 
+0

嗯,我試着你建議得到類似的結果。我更新了我原來的帖子。你能否提供更多方向?我*真的*想要「得到」RoR ... – 2010-10-13 05:28:56

+0

您可以粘貼原始遷移嗎?有創造表的那個人。 – rafamvc 2010-10-14 19:07:22

+0

你有這個數字嗎? – rafamvc 2011-01-07 18:53:49

1

雖然這個問題應固定在schema_definitions.rb ,您可以通過爲NilClass創建to_sym實例方法來擺脫此錯誤。

config/initializers/nil_class.rb下創建Rails應用程序的初始化,這添加到它:

class NilClass 
    def to_sym 
    nil 
    end 
end 

你這樣做之後,嘗試再次運行遷移。