gpt4 book ai didi

ruby-on-rails - db :migrate for an existing model 时出错

转载 作者:行者123 更新时间:2023-12-04 06:34:12 26 4
gpt4 key购买 nike

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

现在我再次使用脚手架 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

最佳答案

尝试

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

rake db:schema:load

参见: Purge or recreate a Ruby on Rails database

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

关于ruby-on-rails - db :migrate for an existing model 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11789867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com