Rails 执行迁移 ArgumentError

Posted

技术标签:

【中文标题】Rails 执行迁移 ArgumentError【英文标题】:Rails Executing migration ArgumentError 【发布时间】:2015-01-26 13:13:32 【问题描述】:

我在执行 rake db:migrate 时遇到了一个奇怪的问题

== 20141127213559 CreateMovies: migrating =====================================
-- create_table(:movies)
   -> 0.0033s
== 20141127213559 CreateMovies: migrated (0.0034s) ============================

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (1 for 0)
ArgumentError: wrong number of arguments (1 for 0)

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我所做的是:

rails generate model Movie

然后编辑迁移 CreateMovies

class CreateMovies < ActiveRecord::Migration
  def up
    #drop_table :movies
    create_table :movies do |t|
      t.string :title
      t.string :rating
      t.text :description
      t.datetime :release_date
      # Add fields that let Rails automatically keep track
      # of when movies are added or modified:
      t.timestamps
    end
  end

  def down
    drop_table :movies
  end
end

这是使用 --trace 执行命令的输出

[fran@arch rottenpotatoes]$ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
== 20141127213559 CreateMovies: migrating =====================================
-- create_table(:movies)
   -> 0.0039s
== 20141127213559 CreateMovies: migrated (0.0041s) ============================

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (1 for 0)/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `initialize'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `new'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `substitute_at'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:97:in `block in substitute_values'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96:in `each'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96:in `each_with_index'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96:in `substitute_values'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:56:in `insert'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:521:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/counter_cache.rb:139:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/attribute_methods/dirty.rb:122:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:306:in `block in _create_record'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `call'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `_run_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:734:in `_run_create_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:306:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/timestamp.rb:57:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:501:in `create_or_update'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `call'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `_run_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:734:in `_run_save_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:302:in `create_or_update'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:142:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/validations.rb:42:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:289:in `block in save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:345:in `block in with_transaction_returning_status'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:218:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:342:in `with_transaction_returning_status'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:289:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:51:in `create!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:1015:in `record_version_state_after_migrating'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:985:in `block in execute_migration_in_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:1030:in `block in ddl_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:218:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:1030:in `ddl_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:983:in `execute_migration_in_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:945:in `block in migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:941:in `each'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:941:in `migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:813:in `up'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:791:in `migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `call'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `block in execute'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `each'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `execute'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:165:in `invoke'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:156:in `invoke_task'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block (2 levels) in top_level'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `each'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block in top_level'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:121:in `run_with_threads'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:106:in `top_level'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:84:in `block in run'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:182:in `standard_exception_handling'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:79:in `run'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/bin/rake:33:in `<top (required)>'
/home/fran/.gem/ruby/2.1.0/bin/rake:23:in `load'
/home/fran/.gem/ruby/2.1.0/bin/rake:23:in `<main>'
ArgumentError: wrong number of arguments (1 for 0)
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `initialize'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `new'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `substitute_at'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:97:in `block in substitute_values'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96:in `each'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96:in `each_with_index'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:96:in `substitute_values'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation.rb:56:in `insert'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:521:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/counter_cache.rb:139:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/attribute_methods/dirty.rb:122:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:306:in `block in _create_record'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `call'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `_run_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:734:in `_run_create_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:306:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/timestamp.rb:57:in `_create_record'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:501:in `create_or_update'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `call'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:88:in `_run_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/callbacks.rb:734:in `_run_save_callbacks'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/callbacks.rb:302:in `create_or_update'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:142:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/validations.rb:42:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:289:in `block in save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:345:in `block in with_transaction_returning_status'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:218:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:342:in `with_transaction_returning_status'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:289:in `save!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/persistence.rb:51:in `create!'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:1015:in `record_version_state_after_migrating'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:985:in `block in execute_migration_in_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:1030:in `block in ddl_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/transactions.rb:218:in `transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:1030:in `ddl_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:983:in `execute_migration_in_transaction'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:945:in `block in migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:941:in `each'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:941:in `migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:813:in `up'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/migration.rb:791:in `migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `call'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:240:in `block in execute'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `each'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:235:in `execute'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/task.rb:165:in `invoke'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:156:in `invoke_task'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block (2 levels) in top_level'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `each'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:112:in `block in top_level'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:121:in `run_with_threads'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:106:in `top_level'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:84:in `block in run'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:182:in `standard_exception_handling'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/lib/rake/application.rb:79:in `run'
/usr/lib/ruby/gems/2.1.0/gems/rake-10.4.0/bin/rake:33:in `<top (required)>'
/home/fran/.gem/ruby/2.1.0/bin/rake:23:in `load'
/home/fran/.gem/ruby/2.1.0/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate

任何帮助将不胜感激。谢谢。

【问题讨论】:

您的迁移文件看起来没问题,您是否再次使用--trace 重新运行迁移以查看完整错误? 我刚刚编辑了初始线程并将输出与--trace 【参考方案1】:

如果您使用的是 Rails 4.2.0.beta4,ActiveRecord 将需要 arel gem。将以下内容添加到您的 gem 文件中

gem 'arel', '6.0.0.beta2'

然后运行

bundle install

您可能会收到以下错误消息

You have requested:arel = 6.0.0.beta2

The bundle currently has arel locked at 6.0.0.
Try running `bundle update arel`

然后

bundle update arel

现在您应该可以运行迁移了。

您可以在这里找到类似的解释性答案:Can't migrate database after scaffold. Section 2.2 Ruby on Rails Tutorial Michael Hartl

【讨论】:

【参考方案2】:

也许你应该在创建电影表后执行这个:

generate migration AddFieldsToMovies newfield:type

这会生成正确的迁移,请参阅:

http://edgeguides.rubyonrails.org/active_record_migrations.html#changing-tables

【讨论】:

以上是关于Rails 执行迁移 ArgumentError的主要内容,如果未能解决你的问题,请参考以下文章

Rails 迁移错误

带有长文本的 Rails 3 迁移

在 Postgres 迁移后,Rails 写入时间增加了 100%

Rails 迁移:self.up 和 self.down 与更改

Rails 迁移总是在模式中插入空格或更改列的顺序

如何在Rails中的迁移中编写SQL