由于迁移错误而尝试重置我的数据库,甚至无法在没有 SQLException 的情况下重置

Posted

技术标签:

【中文标题】由于迁移错误而尝试重置我的数据库,甚至无法在没有 SQLException 的情况下重置【英文标题】:Trying to reset my database due to a bugged migration, can't even reset without SQLException 【发布时间】:2015-10-30 04:18:07 【问题描述】:

我试图修复一个错误的迁移,我最终只是删除了它,但我现在什至无法重置我的 rails db。我只是得到这个错误:

我正在使用这个命令来重置并重新运行:

rake db:reset db:migrate

错误:

rake db:reset rake 中止! ActiveRecord::StatementInvalid: SQLite3::SQLException:没有这样的表:模拟:SELECT "simulations".* FROM "simulations" ORDER BY "simulations"."id" DESC 限制 1 个

这些是我的迁移 1.

 class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              :null => false, :default => "", limit: 96
      t.string :encrypted_password, :null => false, :default => "", limit: 60

      t.timestamps

      t.index :email, unique: true
    end
  end
end

2.

class CreateSimulations < ActiveRecord::Migration
  def change
    # Needs the hash column worked out before this is run 
    create_table :simulations do |t|
        t.integer :x_size
        t.integer :y_size
        t.string :verdict
        t.string :arrangement 
    end

    add_reference :simulations, :user, index: true    
  end
end

3.

    class AddOpinionToSimulation < ActiveRecord::Migration
      def change
        add_column :simulations, :opinion, :hash
      end
    end

最后是我试图运行的坏的启动了这些问题(我删除了文件以阻止它尝试迁移:

class RemoveVerdictFromSimulations < ActiveRecord::Migration
  def change
    remove_column :simulations, :verdict
  end
end

有什么建议吗?

编辑:完整跟踪:

Nathaniels-MacBook-Pro:clio-hiring-ui nathanielmots$ rake db:drop db:create db:migrate rake 中止! ActiveRecord::StatementInvalid: SQLite3::SQLException:没有这样的表:模拟:SELECT "simulations".* FROM "simulations" ORDER BY "simulations"."id" DESC 限制 1 /usr/local/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in initialize' /usr/local/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in 新' /usr/local/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in prepare' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:296:inblock in exec_query' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapter/abstract_adapter.rb:473:in block in log' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in instrument' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapter/abstract_adapter.rb:467:in log' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:293:inexec_query' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:347:in select' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in select_all' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in select_all' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:39:in find_by_sql' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:638:in exec_queries' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:514:in load' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:243:in to_a' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:523:in find_last' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:172:in last' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:3:in last' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/spec/factories/simulations.rb:16:in block (2 levels) in <top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:ininstance_eval' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:in factory' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/spec/factories/simulations.rb:15:in block in ' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in instance_eval' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:inrun' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:7:in define' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/spec/factories/simulations.rb:14:in ' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:inblock in load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in load_dependency' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:inload' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:20:in block (2 levels) in find_definitions' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:19:ineach' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:19:in block in find_definitions' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:15:ineach' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:15:in find_definitions' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl_rails-4.5.0/lib/factory_girl_rails/railtie.rb:21:in block in ' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:36:in call' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:36:in execute_hook' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:45:in block in run_load_hooks' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in each' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in run_load_hooks' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application/finisher.rb:62:in block in ' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:in instance_exec' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:in run' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:55:in block in run_initializers' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:54:in run_initializers' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:352:in initialize!' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/railtie.rb:194:in public_send' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/railtie.rb:194:in method_missing' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/config/environment.rb:5:in ' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in require' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inblock in require' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in load_dependency' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inrequire' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:328:in require_environment!' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:457:in block 在 run_tasks_blocks 的 SQLite3::SQLException 中:没有这样的表: 模拟 /usr/local/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in initialize' /usr/local/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in new' /usr/local/lib/ruby/gems/2.2.0/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in prepare' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:296:inblock in exec_query' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:473:in block in log' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in instrument' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapter/abstract_adapter.rb:467:in log' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:293:inexec_query' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:347:in select' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in select_all' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in select_all' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:39:in find_by_sql' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:638:in exec_queries' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:514:in load' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation.rb:243:in to_a' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:523:in find_last' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:172:in last' /usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/querying.rb:3:in last' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/spec/factories/simulations.rb:16:in block (2 levels) in <top (required)>' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:ininstance_eval' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:18:in factory' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/spec/factories/simulations.rb:15:in block in ' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:in instance_eval' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:49:inrun' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/syntax/default.rb:7:in define' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/spec/factories/simulations.rb:14:in ' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:inblock in load' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in load_dependency' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:inload' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:20:in block (2 levels) in find_definitions' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:19:ineach' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:19:in block in find_definitions' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:15:ineach' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/find_definitions.rb:15:in find_definitions' /usr/local/lib/ruby/gems/2.2.0/gems/factory_girl_rails-4.5.0/lib/factory_girl_rails/railtie.rb:21:in block in ' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:36:in call' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:36:in execute_hook' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:45:in block in run_load_hooks' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in each' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in run_load_hooks' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application/finisher.rb:62:in block in ' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:in instance_exec' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:30:in run' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:55:in block in run_initializers' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/initializable.rb:54:in run_initializers' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:352:in initialize!' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/railtie.rb:194:in public_send' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/railtie.rb:194:in method_missing' /Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/config/environment.rb:5:in ' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in require' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inblock in require' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in load_dependency' /usr/local/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inrequire' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:328:in require_environment!' /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:457:in block 在 run_tasks_blocks 的任务中:TOP => db:migrate => environment

编辑@Ian:

# == Schema Information
#
# Table name: simulations
#
#  id          :integer          not null, primary key
#  x_size      :integer
#  y_size      :integer
#  verdict     :string
#  arrangement :string
#  user_id     :integer
#  opinion     :hash
#

FactoryGirl.define do 
    factory :simulation do |f|
        f.id (Simulation.last.nil? ? 1 : Simulation.last.id + 1)
        f.x_size 3
        f.y_size 3
        f.user_id 1
    end 
end 

【问题讨论】:

是什么让您认为上次迁移“不好”?它应该有单独的 up 和 down 方法,后者用于重新创建列,但删除列是一个完全有效的迁移...... 我认为这很糟糕,因为在执行拼写错误的迁移时通常不会出现“my_sym”错误:***.com/questions/4634557/… 初始化程序中是否有某些东西或正在查询模拟表的东西?其余的回溯可能会提供信息。 为什么叫女工厂?您在堆栈跟踪中有一行非常奇怪:/Users/nathanielmots/Documents/Development/ThemisSolutions/clio-hiring-ui/spec/factories/simulations.rb:14 该文件的第 14 行发生了什么? 【参考方案1】:

db:reset 执行db:drop db:setupdb:setup 执行schema:load,它正在尝试读取您的数据库模式文件,这可能有问题。如果你这样做rake db:drop db:create db:migrate,那应该可以解决它。

此答案中的更多信息:Difference between rake db:migrate db:reset and db:schema:load

【讨论】:

这仍然导致同样的错误.. 你能看出这些迁移有什么问题吗?

以上是关于由于迁移错误而尝试重置我的数据库,甚至无法在没有 SQLException 的情况下重置的主要内容,如果未能解决你的问题,请参考以下文章

由于我没有制作用户表,无法进行迁移

如何重置mysql root密码?

如何重置mysql root密码?

Laravel:在迁移之前删除迁移:重置

Prestashop 迁移 - URL 损坏、重定向错误,并且无法重置管理员密码/访问后端办公室

如何修复由于无法更新项目而导致“错误发生”的SharePoint工作流?