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

**URL:** <https://www.ruby-forum.com/t/newb-here-trying-to-understand-an-issue-with-rolling-back-migrations-then-re-running-them/192434>\
**Category:** Rails\
**Created:** [July 30, 2010, 8:37am UTC](https://www.ruby-forum.com/t/newb-here-trying-to-understand-an-issue-with-rolling-back-migrations-then-re-running-them/192434 "2010-07-30T08:37:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [July 30, 2010, 8:37am UTC](https://www.ruby-forum.com/t/newb-here-trying-to-understand-an-issue-with-rolling-back-migrations-then-re-running-them/192434/1 "2010-07-30T08:37:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [July 30, 2010, 10:23am UTC](https://www.ruby-forum.com/t/newb-here-trying-to-understand-an-issue-with-rolling-back-migrations-then-re-running-them/192434/2 "2010-07-30T10:23:47Z")

</div>

> – create\_table(:sessions)-  
> ‘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

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [July 30, 2010, 1:48pm UTC](https://www.ruby-forum.com/t/newb-here-trying-to-understand-an-issue-with-rolling-back-migrations-then-re-running-them/192434/3 "2010-07-30T13:48:10Z")

</div>

You can run the following command:  
script/dbconsole

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

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [July 31, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/newb-here-trying-to-understand-an-issue-with-rolling-back-migrations-then-re-running-them/192434/4 "2010-07-31T15:23:27Z")

</div>

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.
