1

我正在尝试使用此迁移,但我不断收到错误,例如这些

这是我的迁移文件

class CreateEmployees < ActiveRecord::Migration
  def self.up
    create_table :employees do |t|
      t.string :name 
      t.date :hiredate 
      t.float :salary 
      t.boolean :fulltime , :default => true 
      t.integer :vacationdays 
      t.text :comments
    end 
  end

  def self.down
    drop_table :employees
  end
end

当我尝试执行 rake db:migrate 得到这个错误:

== CreateEmployees:迁移 ============================================== === -- create_table(:employees) rake 中止!发生错误,此迁移和所有后续迁移均已取消:

SQLite3::SQLException: 表 "employees" 已经存在: CREATE TABLE "employees" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar (255), "hiredate" date, "salary" float, "fulltime" boolean默认't',“假期”整数,“评论”文本)/Library/Ruby/Gems/2.0.0/gems/sql ite3-1.3.8/lib/sqlite3/database.rb:91:in initialize' /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in new'/Library /Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in prepare' /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in 执行' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib /active_record/connection_adapters/sqlite3_adapter.rb:328:in block in execute' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:inblock in log' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in instrument' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:inlog'/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:inexecute' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in create_table' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:625:in block in method_missing' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in block in say_with_time' /Library/Ruby/Gems/2.0.0/gems/activerecord -4.0.0/lib/active_record/migration.rb:597:in say_with_time' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:617:in method_missing' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:383:in method_missing' /Users/YuriAguirre/Documents/Aptana Studio 3 Workspace/company/db/migrate/20130923140626_create_employees.rb:3:in up' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:534:in up' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:574:in exec_migration' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0 /lib/active_record/migration.rb:555:in block (2 levels) in migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:554:in block in migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in with_connection' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:553:in 迁移' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:709:in migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:959:in 块中的 execute_migration_in_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord -4.0.0/lib/active_record/migration.rb:1005: block in ddl_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in 在事务块中'/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements。 rb:210:in within_new_transact ion' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in ddl_transaction' /Library/Ruby/Gems/2.0.0/ gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in execute_migration_in_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in block in migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb: 916:在 each' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in 迁移' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in up' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in 迁移' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0 .0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in ' Tasks: TOP => db:migrate (通过使用 --trace 运行任务查看完整跟踪)

4

4 回答 4

2

您的员工表已经在您的数据库中创建。您需要修复它,然后您可以通过两种方式进行修复 1. 删除数据库并再创建一个或 2. 运行向下迁移然后向上迁移

rake db:drop
rake db:create
rake db:migrate
于 2013-09-24T09:50:17.237 回答
1

正如我在评论中所写,您收到该错误的原因是:

SQLite3::SQLException: table "employees" already exists

这仅仅意味着您的数据库中已经有该列。您可以通过以下几种方式解决该问题:

  • 将迁移更改为修改而不是创建:

    class CreateEmployees < ActiveRecord::Migration
      def self.up
        add_column :employees, :name, :string 
        (...)
        end 
      end
    
      def self.down
        remove_column :employees, :name
      end
    end
    
  • 添加另一个将删除整个表的迁移(显然,一个应该具有比修改一个更早的时间戳,因此表将首先被删除然后再次创建):

     class RemoveEmployees < ActiveRecord::Migration
       def change
         drop_table :employees
       end
     end
    
  • 从控制台手动删除表,但这不是一个很好的选择,因为您应该学习使用迁移来更改数据库,当然这将是最快的(如果您知道如何使用控制台)并且不会造成伤害如果这只是一些学习项目,但仍然应该尽可能地学习好习惯。

您可能还想使用较新的语法(AFAIK)def change而不是self.upand self.down,减少输入字母和减少重复;)

于 2013-09-23T15:14:43.650 回答
1

您的迁移未完全运行。您应该回滚迁移并再次迁移。

rails db:rollback
rails db:migrate
于 2019-01-29T10:33:55.107 回答
0

您已经运行了该迁移,但其中有错误。所以迁移进行到一半,创建的表不完整。回滚该迁移,然后再次运行迁移。

于 2013-09-23T15:50:54.617 回答