class CreateInvoiceDetails < ActiveRecord::Migration
def change
create_table :invoice_details do |t|
t.string :invoice_number
t.string :supplier_name
t.date :invoice_due_date
t.string :description_of_goods
t.timestamps
end
end
end
现在我想从invoice_details表中删除description_of_goods列。我使用了以下命令:
rails generate migration RemoveDescriptionOfGoodsFromInvoiceDetails description_of_goods:string
我也尝试过:
rails g migration remove_description_of_goods_from_invoice_details description_of_goods:string
在这两种情况下都会创建迁移,但是当我运行rake db:migrate 时,我面临以下错误:
== 20150319194628 RemoveDescriptionOfGoodsFromInvoiceDetails: migrating =======
-- remove_column(:invoice_details, :description_of_goods, :string)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `to_sym' for nil:NilClass/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/schema_definitions.rb:216:in `column'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:548:in `block (2 levels) in copy_table'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:541:in `each'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:541:in `block in copy_table'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:198:in `create_table'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:in `copy_table'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:531:in `move_table'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:524:in `block in alter_table'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:199:in `transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:523:in `alter_table'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:466:in `remove_column'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:649:in `block in method_missing'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:621:in `block in say_with_time'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:621:in `say_with_time'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:641:in `method_missing'
/home/raj/Desktop/Projects/invoicemanagement/db/migrate/20150319194628_remove_description_of_goods_from_invoice_details.rb:3:in `change'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:595:in `exec_migration'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:578:in `block in migrate'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:577:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:752:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:992:in `block in execute_migration_in_transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:1038:in `block in ddl_transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/transactions.rb:208:in `transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:1038:in `ddl_transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:991:in `execute_migration_in_transaction'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:953:in `block in migrate'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:949:in `each'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:949:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:807:in `up'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/migration.rb:785:in `migrate'
/var/lib/gems/1.9.1/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
请帮我解决这个错误。
答案 0 :(得分:0)
如果您使用的是SQLite,那么SQLite数据库不支持删除列的能力就是一个问题: http://www.sqlite.org/faq.html#q11
答案 1 :(得分:0)
这是我在使用rails迁移时遇到的问题。你的名字错误地拼写或拼错了一个字段。这是你可以做的。
If you have only done one migration since the problem.
rake db:rollback.
That will get you to the mistake and you can change the problem which I will talk about more later.
If it is not the last migration keep going with rake db:rollback until you are there and the problem is fixed.
问题是这样的。注意第一个上的顺序和第二个上的拼写。这两个问题给我的情况与你现在的情况相同,即to_sym bs。
答案 2 :(得分:-2)
我遇到了同样的问题,几个小时后,我终于找到了使用self.up
而不是change
的解决方案
def self.up
create_table :users do |t|
...
end
end
并运行rake db:migrate
。