rename_column 迁移期间生成异常
我正在尝试通过迁移更改我的 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)
I'm trying to change a column in my sqlite DB via a migration...and I have no idea what I'm doing with RoR so please be gentle!
Here's the migration I'm trying to run.
class FixContactIdColumn < ActiveRecord::Migration
def self.up
rename_column :calls, :contact, :contact_id
end
def self.down
end
end
And here's the exception it's generating.
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
Any ideas? I would be happy to provide additional details...
Update: It was suggested that I try to rollback my previous migration...but I get similar results.
-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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管这个问题应该在
schema_definitions.rb
中得到解决,但是您可以通过为NilClass
创建一个to_sym
实例方法来消除这个错误。在您的 Rails 应用程序中的
config/initializers/nil_class.rb
下创建一个初始化程序,并将其添加到其中:完成此操作后,请尝试再次运行迁移。
Though this problem should be fixed in
schema_definitions.rb
, you can get rid of this error by creating ato_sym
instance method forNilClass
.Create an initializer in your rails application under
config/initializers/nil_class.rb
and add this to it:After you've done this, try running your migration again.
请发布您的原始迁移。
如果您的迁移尚未投入生产,只需编辑原始迁移以适合您的需要并运行:
migration_date 是迁移文件名称中的时间戳。另请记住,您必须在计划重新执行的迁移之前选择迁移,因此如果您有:
您想要运行:
Please post your original migration.
If your migration isn't in the production yet, just edit your original migration to fit what you need and run:
the migration_date is the timestamp that is in the name of the migration file. Remember also, you must pick the migration before the one that you plan to re execute, so if you have:
You want to run: