# Need to transfer data from old php like db to rails style db

**URL:** <https://www.ruby-forum.com/t/need-to-transfer-data-from-old-php-like-db-to-rails-style-db/78587>\
**Category:** Rails\
**Created:** [December 7, 2006, 3:45am UTC](https://www.ruby-forum.com/t/need-to-transfer-data-from-old-php-like-db-to-rails-style-db/78587 "2006-12-07T03:45:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 7, 2006, 3:45am UTC](https://www.ruby-forum.com/t/need-to-transfer-data-from-old-php-like-db-to-rails-style-db/78587/1 "2006-12-07T03:45:13Z")

</div>

hey all,  
I have two tables like that from an old php project:  
artists(id,name)  
albums(id,artist\_id,album\_name)

and I need to transfer the data of this database to three tables that  
look like this:  
artists(id,name)  
albums(id,name)  
artists\_albums(album\_id,artist\_id)

any idea what’s the fastest query to do this?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 7, 2006, 4:01am UTC](https://www.ruby-forum.com/t/need-to-transfer-data-from-old-php-like-db-to-rails-style-db/78587/2 "2006-12-07T04:01:17Z")

</div>

On Dec 6, 2006, at 9:42 PM, Patrick A. wrote:

> any idea what’s the fastest query to do this?
> 
> thanx in advance
> 
> Pat

Well, for starters, you probably want albums\_artists if you intend  
this to be a has\_and\_belongs\_to\_many table since “albums” comes  
before “artists”

rename artists to old\_artists  
rename albums to old\_albums

insert into artists select \* from old\_artists  
insert into albums select id, album\_name as name from old\_albums  
insert into albums\_artists select id as album\_id, artist\_id from  
old\_albums

then if all your test pass 😉 you can drop the old\_\* tables

You didn’t say what database so I’m leaving this generic. (and  
migrations are great for this sort of thing, too)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 7, 2006, 4:07am UTC](https://www.ruby-forum.com/t/need-to-transfer-data-from-old-php-like-db-to-rails-style-db/78587/3 "2006-12-07T04:07:17Z")

</div>

On 12/7/06, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > albums(id,name)  
> > before “artists”
> 
> You didn’t say what database so I’m leaving this generic. (and  
> migrations are great for this sort of thing, too)
> 
> -Rob

thanx a lot!
