rake db:migrate
堆栈跟踪
> == 20160315111403 Books: migrating ============================================
> -- create_table(:books) rake aborted! An error has occurred, all later migrations canceled:
Error:
> Mysql2::Error: Table 'books' already exists: CREATE TABLE `books`
> (`id` int(11) auto_increment PRIMARY KEY, `title` varchar(32) NOT
> NULL, `price` float, `subject_id` int(11), `description` text,
> `created_at` datetime)
> ENGINE=InnoDB/home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305:in
> `query'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305:in
> `block in execute'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in
> `instrument'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305:in
> `execute'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/mysql2_adapter.rb:231:in
> `execute'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:208:in
> `create_table'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:468:in
> `create_table'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:662:in
> `block in method_missing'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:631:in
> `block in say_with_time'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:631:in
> `say_with_time'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:651:in
> `method_missing'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:416:in
> `method_missing'
> /home/paritosh/library/db/migrate/20160315111403_books.rb:4:in `up'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:568:in
> `up'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:608:in
> `exec_migration'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:589:in
> `block (2 levels) in migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:588:in
> `block in migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in
> `with_connection'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:587:in
> `migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in
> `migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:995:in
> `block in execute_migration_in_transaction'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:1043:in
> `ddl_transaction'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:994:in
> `execute_migration_in_transaction'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:956:in
> `block in migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in
> `each'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in
> `migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in
> `up'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in
> `migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in
> `migrate'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in
> `block (2 levels) in <top (required)>'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/task.rb:246:in
> `call'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/task.rb:246:in
> `block in execute'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/task.rb:241:in
> `each'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/task.rb:241:in
> `execute'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/task.rb:184:in
> `block in invoke_with_call_chain'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/task.rb:177:in
> `invoke_with_call_chain'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/task.rb:170:in
> `invoke'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:143:in
> `invoke_task'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:101:in
> `block (2 levels) in top_level'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:101:in
> `each'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:101:in
> `block in top_level'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:110:in
> `run_with_threads'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:95:in
> `top_level'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:73:in
> `block in run'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:160:in
> `standard_exception_handling'
> /home/paritosh/.rvm/gems/ruby-2.2.3/gems/rake-10.0.4/lib/rake/application.rb:70:in
> `run'
> /home/paritosh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in
> `eval'
> /home/paritosh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in
> `<main>' Tasks: TOP => db:migrate (See full trace by running task with
> --trace)
答案 0 :(得分:1)
我试试这个:
$ rake db:drop
$ rake db:create
$ rake db:migrate
rake db:drop
会丢弃当前数据库。如果您已为环境生产,开发和测试生成数据库,则可能需要运行rake db:drop:all
以使用单个命令删除所有这些数据库,以便稍后重新创建它们。例如,如果要丢弃开发数据库,则可以运行RAILS_ENV=development rake db:drop
。
rake db:create
将再次创建数据库。然后,如果您想为所有环境(生产,开发和测试)创建数据库,您可以运行rake db:create:all
。
最后,
rake db:migrate
将执行自您创建数据库模型以来生成的所有迁移。这将使您当前的数据库处于最新状态。
这三个操作将为您提供一个全新的数据库,您将不会看到这个&#34;表已经存在&#34;当你再次运行迁移时。
顺便说一下,如果您已经使用db / seed.rb插入数据,那么是时候再次运行rake db:seed
来填充数据库。
答案 1 :(得分:0)
使用此代码:
rake db:rollback or rake db:migrate:down VERSION=<timestamp>
然后使用rake db:migrate
答案 2 :(得分:0)