2013-09-23 153 views
1

我試着去使用這種遷移,但我不斷收到錯誤,像這些錯誤遷移DB Rails的

這是我的移民文件

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 

當我嘗試執行耙分貝:遷移得到這個錯誤:

== CreateEmployees: migrating ================================================ -- create_table(:employees) rake aborted! An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "employees" already exists: CREATE TABLE "employees" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar (255), "hiredate" date, "salary" float, "fulltime" boolean DEFAULT 't', "vacationdays" integer, "comments" text) /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 execute' /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:in block 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:in log' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:in execute' /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 migrate' /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 block in execute_migration_in_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in 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 block in transactio n' /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:in each' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in migrate' /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 migrate' /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 (See full trace by running task with --trace)

+0

您已收到答案:'的SQLite3 ::的SQLException:你已經提供了足夠的細節來判斷你爲什麼創建了這個表,也許你已經手工創建了它,也許它是由另一個遷移創建的,誰知道。 – zrl3dx

+0

哦,現在我看到了,我是Rails的新手,我如何重新創建或刪除已存在的表? –

+0

好辦法是使用遷移對db進行的所有更改,以便以後不會丟失。我不知道你是想從頭開始創建表還是修改它,後一個應該使用'add_column:table_name,:column_name,:column_type'語法 – zrl3dx

回答

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 
    
  • 降表,但是這並不是一個非常:修改一個這樣的表將首先被再度回落,然後創建)不錯的選擇,因爲你應該學習使用遷移來改變你的數據庫,當然這將是最快的一個(如果你知道如何使用控制檯)並且不會造成任何傷害,但如果這只是一些學習項目,儘可能地學習良好的習慣。

你也可能需要使用新的語法(據我所知)的def change代替self.upself.down,少字母鍵入並減少重複;)

0

你跑了,移民已經在它的錯誤。所以遷移進行到一半,創建表不完整。回滾該遷移,然後再次運行遷移。

2

您的員工表已經創建在您的數據庫中。你需要修復它,那麼你可以做到這一點有兩種方式1.降數據庫,並創建一個或多個2.運行向下遷移然後向上遷移

rake db:drop 
rake db:create 
rake db:migrate