我試着去使用這種遷移,但我不斷收到錯誤,像這些錯誤遷移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:inprepare' /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:inblock 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:ininstrument' /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: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:inblock 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:insay_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:inmethod_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:inup' /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:inblock (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:inwith_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:inmigrate' /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:inblock 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:inwithin_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:intransaction' /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:inexecute_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:ineach' /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:inup' /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)
您已收到答案:'的SQLite3 ::的SQLException:你已經提供了足夠的細節來判斷你爲什麼創建了這個表,也許你已經手工創建了它,也許它是由另一個遷移創建的,誰知道。 – zrl3dx
哦,現在我看到了,我是Rails的新手,我如何重新創建或刪除已存在的表? –
好辦法是使用遷移對db進行的所有更改,以便以後不會丟失。我不知道你是想從頭開始創建表還是修改它,後一個應該使用'add_column:table_name,:column_name,:column_type'語法 – zrl3dx