3

我在 rake db:migrate 中遇到问题。当我使用脚手架创建模型,运行 db:migrate 然后使用脚手架 d 帖子删除模型时,问题就开始了。

现在我再次使用脚手架 g 创建了模型,并运行了 db:migrate。由于用户表已经存在,它给了我一个错误。我该如何补救?

murtaza@murtaza-dev:~/workspace/rails/base$ rake db:migrate
==  CreatePosts: migrating ====================================================
-- create_table(:posts) rake aborted! An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "posts" already exists: CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "content" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 

Tasks: TOP => db:migrate (See full trace by running task with --trace) murtaza@murtaza-dev:~/workspace/rails/base$ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:migrate
==  CreatePosts: migrating ====================================================
-- create_table(:posts) rake aborted! An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "posts" already exists: CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "content" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)  /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `initialize' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `new' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in `prepare' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:134:in `execute' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `block in execute' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `execute' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:466:in `block in method_missing' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:438:in `block in say_with_time' /home/murtaza/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:280:in `measure' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:438:in `say_with_time' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:458:in `method_missing' /home/murtaza/workspace/rails/base/db/migrate/20120802061742_create_posts.rb:3:in `change' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:407:in `block (2 levels) in migrate' /home/murtaza/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:280:in `measure' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:407:in `block in migrate' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in `with_connection' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:389:in `migrate' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:528:in `migrate' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:720:in `block (2 levels) in migrate' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:775:in `call' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:775:in `block in ddl_transaction' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in `transaction' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:775:in `ddl_transaction' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:719:in `block in migrate' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:700:in `each' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:700:in `migrate' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:570:in `up' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/migration.rb:551:in `migrate' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain' /home/murtaza/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/bin/rake:23:in `load' /home/murtaza/.rvm/gems/ruby-1.9.3-p194/bin/rake:23:in `<main>' Tasks: TOP => db:migrate
4

2 回答 2

6

尝试

rake db:drop && rake db:create && rake db:migrate && rake db:schema:dump && rake db:test:prepare

或者

rake db:schema:load

请参阅: 清除或重新创建 Ruby on Rails 数据库

多次迁移不止一次创建用户表。您可以手动查看每个迁移并删除创建表查询。

于 2012-08-03T05:48:50.163 回答
1

AFAIKscaffold d不会生成用于删除表的迁移,您必须自己执行此操作。您可以手动添加迁移以删除表:

def up
    drop_table :posts
end
def down
    raise ActiveRecord::IrreversibleMigration
end

并且rake db:migrate,只需确保它出现在您尝试posts再次创建的新迁移之前。sqlite3或者,您可以使用命令行工具打开数据库,然后drop table posts;手动删除表。

于 2012-08-03T05:54:07.050 回答