1

Im trying to use this migration but i keep getting the error, like these

This is my Migration file

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

when i try to execute rake db:migrate get this error:

== 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: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: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)

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
Ytipsh
  • 37
  • 2
  • 10
  • You've received answer: `SQLite3::SQLException: table "employees" already exists` You've provided not enough details to judge why you have this table created, maybe you've made it by hand, maybe it was created by another migration, who knows. – zrl3dx Sep 23 '13 at 14:32
  • Oh now i saw, im a newbie at Rails how can i recreate or drop that table that already exist? – Ytipsh Sep 23 '13 at 14:34
  • The Good Way is to use migrations for all changes made to db, so you won't get lost later. I don't know if you want to create that table from scratch or modify it, it latter one you should use `add_column :table_name, :column_name, :column_type` syntax – zrl3dx Sep 23 '13 at 14:40
  • Oh, got it, but i still don't know how i can drop that table now and create the table again haha cause i have no ideia if i have created the table with the right statement. – Ytipsh Sep 23 '13 at 14:42
  • You've got answer in migration you provided ;) Just write `drop_table :employees` in `self.up` part (or `change` instead of both `self.up` and `self.down`) or remove it by hand from sqlite console. – zrl3dx Sep 23 '13 at 14:44
  • Oh ok! did it and work! thx man! – Ytipsh Sep 23 '13 at 15:01
  • Ok, so I will add this as an answer so can mark it as answered and question will disappear from unanswered ones. – zrl3dx Sep 23 '13 at 15:03

4 Answers4

2

Your employees tables is already created in your database. You need to fix it then you can do it in two ways 1. drop the database and create one more or 2. run the down migration then up migration

rake db:drop
rake db:create
rake db:migrate
Sandip Mondal
  • 921
  • 7
  • 12
1

As I've written in comment, you are receiving that error because of:

SQLite3::SQLException: table "employees" already exists

Which simply means you already have that column in your database. You can resolve that in few ways:

  • changing migration to modify instead of create:

    class CreateEmployees < ActiveRecord::Migration
      def self.up
        add_column :employees, :name, :string 
        (...)
        end 
      end
    
      def self.down
        remove_column :employees, :name
      end
    end
    
  • add another migration which will drop whole table (obviously, that one should be with earlier timestamp than modifying one so table will be dropped first and then created again):

     class RemoveEmployees < ActiveRecord::Migration
       def change
         drop_table :employees
       end
     end
    
  • drop table by hand from console, but that's not a very good option as you should learn to use migrations for altering your database, of course that w'd be the fastest one (if you know how to use console) and will make no harm if that's just some learning project, but still, you should learn good habits when you can.

You might also want to use newer syntax (AFAIK) def change instead of self.up and self.down, less letters to type and less duplication ;)

zrl3dx
  • 7,699
  • 3
  • 25
  • 35
1

Your migration is not completely run.You should rollback migration and migrate again.

rails db:rollback
rails db:migrate
Foram
  • 483
  • 5
  • 12
0

You ran that migration already with an error in it. So the migration ran halfway, creating the table incompletely. Rollback that migration, and then run the migration again.

bigpotato
  • 26,262
  • 56
  • 178
  • 334