# Migration with data

**URL:** <https://www.ruby-forum.com/t/migration-with-data/153266>\
**Category:** Rails\
**Created:** [November 14, 2008, 5:17am UTC](https://www.ruby-forum.com/t/migration-with-data/153266 "2008-11-14T05:17:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 14, 2008, 5:17am UTC](https://www.ruby-forum.com/t/migration-with-data/153266/1 "2008-11-14T05:17:28Z")

</div>

Hi  
I am using postgres and have two migration files like  
001\_create\_categories.rb as  
class CreateCategories \< ActiveRecord::Migration  
def self.up  
create\_table :categories do |t|  
t.column :name, :string, :limit=\>80  
end  
end

def self.down  
drop\_table :categories  
end  
end  
and 002\_populate\_categories.rb

class PopulateCategories \< ActiveRecord::Migration  
execute ‘ALTER SEQUENCE categories\_id\_seq RESTART WITH 100;’  
NAMES = [“Category 1”, “Category 2”, “Category 3”]  
def self.up  
NAMES.each{|name| Category.create(:name =\> name)}  
end

def self.down  
NAMES.each{|name| Category.find\_by\_name(name).destroy}  
end  
end

```
 My problem is if I seperately done these migrations ie one by one

```

everything is ok .I get sequence starts with 100 and the values like  
100 Category1  
101 Category2  
102 Category3  
But if I just migrate two migrations at once I get error

– execute(“ALTER SEQUENCE categories\_id\_seq RESTART WITH 100;”)  
rake aborted!  
RuntimeError: ERROR C42P01 Mrelation “categories\_id\_seq” does not  
exist Fnamespace.c L273 RRangeVarGetRelid: ALTER SEQUENCE  
categories\_id\_seq RESTART WITH 100;

```
   Could you please tell why this happens?

```

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 17, 2008, 11:12am UTC](https://www.ruby-forum.com/t/migration-with-data/153266/2 "2008-11-17T11:12:48Z")

</div>

Hi  
Still I could not solve this problem  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 17, 2008, 11:34am UTC](https://www.ruby-forum.com/t/migration-with-data/153266/3 "2008-11-17T11:34:27Z")

</div>

On 14 Nov 2008, at 04:17, Sijo Kg wrote:

> ```
> NAMES.each{|name| Category.create(:name => name)}
> 
> ```
> 
> end
> 
> def self.down  
> NAMES.each{|name| Category.find\_by\_name(name).destroy}  
> end

Move that execute into the self.up method: where it currently is, it  
is executed when the file is loaded. Since ActiveRecord loads all the  
migrations to run up front it will run that execute before migration  
1 as run.

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 17, 2008, 11:50am UTC](https://www.ruby-forum.com/t/migration-with-data/153266/4 "2008-11-17T11:50:19Z")

</div>

Hi  
Thanks for your reply  
Sijo
