在为One Mont Rails教程设置针脚支架时,我遇到了rake db:migrate
的一些问题。当我尝试在终端中迁移时,我收到以下错误:
Taylors-MacBook-Pro:pinteresting taylorburton$ bin/rake db:migrate RAILS_ENV=development
== 20150410031405 AddUserIdToPins: migrating ==================================
-- add_column(:pins, :user_id, :integer)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: user_id: ALTER TABLE "pins" ADD "user_id" integer/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:134:in `execute'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:333:in `block in execute'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598@global/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:333:in `execute'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:381:in `add_column'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:458:in `add_column'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:662:in `block in method_missing'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `block in say_with_time'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `say_with_time'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:652:in `method_missing'
/Users/taylorburton/Desktop/pinteresting/db/migrate/20150410031405_add_user_id_to_pins.rb:3:in `change'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:606:in `exec_migration'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:589:in `block in migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:588:in `migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:765:in `migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in `block in ddl_transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in `ddl_transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598@global/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598@global/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598@global/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/taylorburton/.rvm/gems/ruby-2.0.0-p598@global/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Taylors-MacBook-Pro:pinteresting taylorburton$
答案 0 :(得分:0)
仔细阅读错误消息。它说:
duplicate column name: user_id: ALTER TABLE "pins" ADD "user_id"
这意味着在执行迁移时,user_id
表中已存在pins
,这是意外的。因此错误。
如果从user_id
表中删除pins
列,则会出现此错误,希望迁移成功。但是手动(我的意思是使用SQL命令)不建议改变Rails数据库,实际上这本身很可能是这个错误背后的原因。
或者,如果您不关心任何现有数据,您也可以这样做:
rake db:reset
后跟rake db:migrate