我在运行rake db:migrate
创建新表时遇到问题(由rails g model
生成的迁移文件)。
class CreateReminderLogs < ActiveRecord::Migration
def change
create_table :reminder_logs do |t|
t.string :remindable_type
t.integer :remindable_id
t.integer :user_id
t.string :remindable_email
t.datetime :reminder_sent_at
t.timestamps
end
add_index :reminder_logs, :user_id
add_index :reminder_logs, :remindable_id
end
end
在我的命令中添加--trace
选项后,它会显示堆栈,如下所示。似乎错误与外键有关,但我没有在此迁移中指定任何外键属性。
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
== 20151119105903 CreateReminderLogs: migrating ===============================
-- create_table(:reminder_logs)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
wrong number of arguments (4 for 1..2)/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/table_definition.rb:4:in `foreign_key'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/table_definition.rb:159:in `add_foreign_key'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/column_options_handler.rb:24:in `schema_plus_handle_column_options'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/table_definition.rb:127:in `column_with_schema_plus'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_definitions.rb:234:in `block (3 levels) in <class:TableDefinition>'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_definitions.rb:234:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_definitions.rb:234:in `block (2 levels) in <class:TableDefinition>'
/Users/nayiaw/Documents/workspace/my-resipi/db/migrate/20151119105903_create_reminder_logs.rb:5:in `block in change'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:16:in `call'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:16:in `block in create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:42:in `block in create_table_with_schema_plus'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_statements.rb:198:in `create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:37:in `create_table_with_schema_plus'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:14:in `create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:460:in `create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:656:in `block in method_missing'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:628:in `block in say_with_time'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/2.1.0/benchmark.rb:279:in `measure'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:628:in `say_with_time'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:648:in `method_missing'
/Users/nayiaw/Documents/workspace/my-resipi/db/migrate/20151119105903_create_reminder_logs.rb:3:in `change'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:602:in `exec_migration'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:586:in `block (2 levels) in migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/2.1.0/benchmark.rb:279:in `measure'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:585:in `block in migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:584:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:759:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:1046:in `ddl_transaction'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:959:in `block in migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:955:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:955:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:814:in `up'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:792:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/nayiaw/.rbenv/versions/2.1.2/bin/rake:23:in `load'
/Users/nayiaw/.rbenv/versions/2.1.2/bin/rake:23:in `<main>'
ArgumentError: wrong number of arguments (4 for 1..2)
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/table_definition.rb:4:in `foreign_key'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/table_definition.rb:159:in `add_foreign_key'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/column_options_handler.rb:24:in `schema_plus_handle_column_options'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/table_definition.rb:127:in `column_with_schema_plus'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_definitions.rb:234:in `block (3 levels) in <class:TableDefinition>'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_definitions.rb:234:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_definitions.rb:234:in `block (2 levels) in <class:TableDefinition>'
/Users/nayiaw/Documents/workspace/my-resipi/db/migrate/20151119105903_create_reminder_logs.rb:5:in `block in change'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:16:in `call'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:16:in `block in create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:42:in `block in create_table_with_schema_plus'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/schema_statements.rb:198:in `create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/schema_plus-1.5.1/lib/schema_plus/active_record/connection_adapters/schema_statements.rb:37:in `create_table_with_schema_plus'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:14:in `create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:460:in `create_table'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:656:in `block in method_missing'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:628:in `block in say_with_time'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/2.1.0/benchmark.rb:279:in `measure'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:628:in `say_with_time'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:648:in `method_missing'
/Users/nayiaw/Documents/workspace/my-resipi/db/migrate/20151119105903_create_reminder_logs.rb:3:in `change'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:602:in `exec_migration'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:586:in `block (2 levels) in migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/2.1.0/benchmark.rb:279:in `measure'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:585:in `block in migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:584:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:759:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:1046:in `ddl_transaction'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:959:in `block in migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:955:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:955:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:814:in `up'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/migration.rb:792:in `migrate'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.13/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/Users/nayiaw/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/nayiaw/.rbenv/versions/2.1.2/bin/rake:23:in `load'
/Users/nayiaw/.rbenv/versions/2.1.2/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate
我认为这个问题与this SO question类似,但从堆栈跟踪来看它似乎有所不同。我已经尝试将arel gem更新为版本&gt; = 6.0.3但它有兼容性错误。
Bundler could not find compatible versions for gem "arel":
In Gemfile:
default_value_for (~> 3.0.0) ruby depends on
activerecord (< 5.0, >= 3.2.0) ruby depends on
arel (~> 5.0.0) ruby
arel (>= 6.0.3, ~> 6.0) ruby
这个项目的Rails版本是'4.1.13'。
请帮助我,因为我在网上找不到任何解决方案。提前谢谢。
答案 0 :(得分:1)
SchemaPlus gem似乎干扰了您添加代码的索引。您可以将迁移调整为:
class CreateReminderLogs < ActiveRecord::Migration
def change
create_table :reminder_logs do |t|
t.string :remindable_type
t.integer :remindable_id, index: true
t.integer :user_id, index: true
t.string :remindable_email
t.datetime :reminder_sent_at
t.timestamps
end
end
end