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) レーキが中止されました! エラーが発生しました。これ以降の移行はすべてキャンセルされました:

SQLite3::SQLException: テーブル "employees" は既に存在します: 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: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' block in method_missing' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:625: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' / up' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:574:in ライブラリ/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:534:in exec_migration' /ライブラリ/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: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: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 transaction' /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' transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in ddl_transaction' /Library/Ruby/Gems/2.0.0/ gems/activerecord-4.0.0/lib/active_record/migration.rb:958:migration execute_migration_in_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in のブロック内' /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 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 `ブロック (2 レベル) in ' タスク: TOP => db:migrate (タスクを --trace で実行して完全なトレースを表示)

4

4 に答える 4

2

従業員テーブルは、データベースに既に作成されています。修正する必要がある場合は、次の 2 つの方法で実行できます。1. データベースを削除してもう 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
    
  • コンソールから手動でテーブルを削除しますが、データベースを変更するために移行を使用する方法を学ぶ必要があるため、これはあまり良いオプションではありません。もちろん、コンソールの使用方法を知っている場合は、これが最も高速であり、害はありません。それが単なる学習プロジェクトである場合でも、可能な場合は良い習慣を学ぶ必要があります.

また、 anddef changeの代わりに新しい構文 (AFAIK) を使用することもできます。入力する文字が少なく、重複も少なくなります;)self.upself.down

于 2013-09-23T15:14:43.650 に答える
0

その移行は既に実行されていますが、エラーが発生しています。そのため、移行は途中で実行され、テーブルが不完全に作成されました。その移行をロールバックしてから、移行を再度実行します。

于 2013-09-23T15:50:54.617 に答える