gpt4 book ai didi

ruby-on-rails - 在rename_column迁移期间生成的异常

转载 作者:行者123 更新时间:2023-12-02 04:13:25 26 4
gpt4 key购买 nike

我正在尝试通过迁移来更改sqlite DB中的列...而且我不知道我在用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)

最佳答案

尽管应该在schema_definitions.rb中解决此问题,但是您可以通过为to_sym创建NilClass实例方法来摆脱此错误。

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

class NilClass
def to_sym
nil
end
end

完成此操作后,请尝试再次运行迁移。

关于ruby-on-rails - 在rename_column迁移期间生成的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3920443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com