# Populating from existing tables with migration

**URL:** <https://www.ruby-forum.com/t/populating-from-existing-tables-with-migration/116671>\
**Category:** Rails\
**Created:** [October 8, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/populating-from-existing-tables-with-migration/116671 "2007-10-08T17:39:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [October 8, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/populating-from-existing-tables-with-migration/116671/1 "2007-10-08T17:39:54Z")

</div>

Hi all,

I was wondering how to create a rails migration which, from one  
preexisting and populated table, creates two new tables, each populated  
with a subset of the columns of the old, big table. I suppose it is  
possible and easy, but I havenÂ´t found any example out there.

Thanks,  
Alvaro.

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [October 8, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/populating-from-existing-tables-with-migration/116671/2 "2007-10-08T17:46:58Z")

</div>

On Oct 8, 2007, at 10:39 , Alvaro P. wrote:

> I was wondering how to create a rails migration which, from one  
> preexisting and populated table, creates two new tables, each  
> populated  
> with a subset of the columns of the old, big table.

You can do this by executing raw SQL in the migration (via the  
execute method). I don’t know if you can do it using the migration DSL.

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [October 8, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/populating-from-existing-tables-with-migration/116671/3 "2007-10-08T18:22:48Z")

</div>

On 10/8/07, Alvaro P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I was wondering how to create a rails migration which, from one  
> preexisting and populated table, creates two new tables, each populated  
> with a subset of the columns of the old, big table. I suppose it is  
> possible and easy, but I haven´t found any example out there.

Rails doesn’t have a “create table as select” construct like some  
databases have. You can use ActiveRecord::Base.execute to run any  
arbitrary SQL you wish. However, this makes your migration very  
database-specific.

If you want to stick with the migration DSL, you need to create the  
new tables, then load the data (you can use any of the AR methods like  
find, create, etc.), and finally drop the old table.

If you’re going to use AR methods, you should define “empty” model  
classes for each of the three tables inside your migration class, so  
your migration doesn’t trip over validations, associations, etc.

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [October 8, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/populating-from-existing-tables-with-migration/116671/4 "2007-10-08T19:13:53Z")

</div>

On 10/8/07, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

tabase-specific.

> If you want to stick with the migration DSL, you need to create the  
> new tables, then load the data (you can use any of the AR methods like  
> find, create, etc.), and finally drop the old table.
> 
> If you’re going to use AR methods, you should define “empty” model  
> classes for each of the three tables inside your migration class, so  
> your migration doesn’t trip over validations, associations, etc.

This also ensures that old migrations won’t break as the REAL models  
code evolves.

The models don’t even need to be ‘empty’ I’ve found it useful to add  
methods to the models 're’defined in a migration for some  
manipulations.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
