newb here trying to understand an issue with rolling back migrations then re-running them

I’m sure this is total newb (since I am pretty newb) but I’m curious what the issue is here. I attempt to role back all my migrations which I’m doing with

rake db:migrate VERSION=0
(in /Users/rick/projects/rails/sillymeters)

== CreateSessions: never migrated, skipping ==================================

== CreateUsers: reverting ====================================================
– drop_table(:users)
-> 0.0011s
== CreateUsers: reverted (0.0012s) ===========================================

== CreateMeters: reverting ===================================================
– drop_table(:meters)
-> 0.0008s
== CreateMeters: reverted (0.0009s) ==========================================

etc…

Note the first one says “CreateSessions: never migrated, skipping”

When I then go to run the migrations, most go through fine, but then I get the following error when it tries to run the CreateSessions migration:

~/projects/rails/sillymeters(master) $ rake db:migrate
(in /Users/rick/projects/rails/sillymeters)
== CreateSessions: migrating =================================================
– create_table(:sessions)-

rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table “sessions” already exists: CREATE TABLE “sessions” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, “session_id” varchar(255) NOT NULL, “data” text, “created_at” datetime, “updated_at” datetime)

I’m assuming that each application has its own instance of SQLLite so I’m curious how this error is occurring or what I need to do to fix it. I ‘think’ I might be getting this because it’s possible I did at one point have a similar migration that created the Session table, but then I deleted that migration and created it as a new one with a new name. The table name was the same though “sessions” so I don’t get what is going on.

The migration in question looks like:

class CreateSessions < ActiveRecord::Migration
def self.up
create_table :sessions do |t|
t.string :session_id, :null => false
t.text :data

  t.timestamps
end

add_index :sessions, :session_id
add_index :sessions, :updated_at

end

def self.down
drop_table :sessions
end
end

Note the first one says "CreateSessions: never migrated, skipping"

When I then go to run the migrations, most go through fine, but then I get
the following error when it tries to run the CreateSessions migration:

~/projects/rails/sillymeters(master) $ rake db:migrate
(in /Users/rick/projects/rails/sillymeters)
== CreateSessions: migrating

-- create_table(:sessions)-
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "sessions" already exists: CREATE TABLE
"sessions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "session_id"
varchar(255) NOT NULL, "data" text, "created_at" datetime, "updated_at"
datetime)

I'm assuming that each application has its own instance of SQLLite so I'm
curious how this error is occurring or what I need to do to fix it. I
'think' I might be getting this because it's possible I did at one point
have a similar migration that created the Session table, but then I deleted
that migration and created it as a new one with a new name. The table name
was the same though "sessions" so I don't get what is going on.

Rails tracks migrations that have be run with the schema_migrations
(which contains the numerical identifier for each migration). If you
created a new migration then that migration won't be recorded as
having being run (which is why migrate VERSION=0 doesn't do anything)
but the table is of course still there, so trying to create it again
raises an error

Fred

You can run the following command:

script/dbconsole

And then manually drop the sessions table (drop sessions). Then try full rollbacks and migrations again.

Check to see if you have duplicate code in any of your migrations to
create this table:

SQLite3::SQLException: table "sessions" already exists: CREATE TABLE
"sessions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "session_id"
varchar(255) NOT NULL, "data" text, "created_at" datetime, "updated_at"
datetime)

This can easily happen if you have code to create all your tables in
the main CreateDatabase migration file, then run script/generate to
generate scaffolding or a model for an existing table. The generator
script will create a new migration with table creation code, causing
rake to choke the next time you do a migration.