# Can I create models from a preexisiting database?

**URL:** <https://www.ruby-forum.com/t/can-i-create-models-from-a-preexisiting-database/169369>\
**Category:** Rails\
**Created:** [June 2, 2009, 10:10pm UTC](https://www.ruby-forum.com/t/can-i-create-models-from-a-preexisiting-database/169369 "2009-06-02T22:10:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![user\_zero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/82dd89/32.png) [@user\_zero](https://www.ruby-forum.com/u/user_zero)\
**Post date:** [June 2, 2009, 10:10pm UTC](https://www.ruby-forum.com/t/can-i-create-models-from-a-preexisiting-database/169369/1 "2009-06-02T22:10:37Z")

</div>

Hello All this is my first post here, I have been trying to learn rails  
and kept hitting dead ends because of outdated tutorials then I went  
through the official one and what do you know it works! 😉 .

I’m sure this is a noob question but I can’t find the answer searching  
for it for some reason. Can I do a reverse migration where instead of  
creating the database scema based on my models, I generate models  
(migrations and controllers too) based on the database I already have  
laid out (MySql). I remember trying one tutorial a few months ago that  
did it this way but then none of the C.R.U.D. features worked right. I’m  
using the latest official versions ruby 1.8.7 and RoR 2.3 .

Thanks.

---

<div class="post-metadata">

**Author:** ![user\_zero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/82dd89/32.png) [@user\_zero](https://www.ruby-forum.com/u/user_zero)\
**Post date:** [June 3, 2009, 12:11am UTC](https://www.ruby-forum.com/t/can-i-create-models-from-a-preexisiting-database/169369/2 "2009-06-03T00:11:54Z")

</div>

removed\_email\_address@domain.invalid Huh wrote:

> using the latest official versions ruby 1.8.7 and RoR 2.3 .
> 
> Thanks.

I don’t know of any automatic way to generate models from an existing  
schema, but as far as I know, there’s nothing to stop you generating  
models normally using the existing database structure.

I guess you can then just “forget” to run the migrations, and your  
models will work with your existing data.

It might not be totally the best way to do it, but it should work.

HTH

~Matt

---

<div class="post-metadata">

**Author:** ![user\_zero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/82dd89/32.png) [@user\_zero](https://www.ruby-forum.com/u/user_zero)\
**Post date:** [June 3, 2009, 12:26am UTC](https://www.ruby-forum.com/t/can-i-create-models-from-a-preexisiting-database/169369/3 "2009-06-03T00:26:22Z")

</div>

Matt H. wrote:

> I don’t know of any automatic way to generate models from an existing  
> schema, but as far as I know, there’s nothing to stop you generating  
> models normally using the existing database structure.

Right. However, you’ll want an accurate schema.rb file anyway. Use  
rake db:schema:dump to generate one.

> I guess you can then just “forget” to run the migrations, and your  
> models will work with your existing data.

No need to “forget”. Use --skip-migration, or delete the migrations  
without running them, or don’t use the model generator at all!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
