本文介绍了Rails 执行迁移 ArgumentError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a strange problem executing 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

And then edit the migration 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 执行命令的输出

This is the output of executing the command with --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

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

Any help would be really appreciated. Thanks.

推荐答案

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

If you are using Rails 4.2.0.beta4, ActiveRecord will require arel gem. Add the following to your gem file

gem 'arel', '6.0.0.beta2'

然后运行

bundle install

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

You may get below error message

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

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

Now you should able to run migration.

在这里您可能会找到类似的解释性答案:脚手架后无法迁移数据库.第 2.2 节 Ruby on Rails 教程 Michael Hartl

Here you may find similar and explanatory answer: Can't migrate database after scaffold. Section 2.2 Ruby on Rails Tutorial Michael Hartl

这篇关于Rails 执行迁移 ArgumentError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:29