# How to use an EXISTing DB-Schema in my Rails APP..?

**URL:** <https://www.ruby-forum.com/t/how-to-use-an-existing-db-schema-in-my-rails-app/151627>\
**Category:** Rails\
**Created:** [October 23, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/how-to-use-an-existing-db-schema-in-my-rails-app/151627 "2008-10-23T16:02:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepFritz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@deepFritz](https://www.ruby-forum.com/u/deepFritz)\
**Post date:** [October 23, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/how-to-use-an-existing-db-schema-in-my-rails-app/151627/1 "2008-10-23T16:02:26Z")

</div>

Hi friends,

We have JUST started with Ruby/Rails…

and we are very much impressed with the scaffold generator which gets  
us kick-started by creating even the DB-Schema Migration scripts fer  
us, which is DB agnostic…!!

But then, me & my friends began wondering…

– WHAT if we had ALREADY designed a DB-Schema & even made the Tables,  
etc.??  
(or , more realistically, GIVEN an existing schema info…!)

– WHAT if this schema did NOT follow the “Conventions” that Rails  
NEEDS in order to infer details regarding the Models, Controllers,  
etc…?!?

Will be grateful if anyone could give us some pointers in this  
regard…

Cheers,

---

<div class="post-metadata">

**Author:** ![deepFritz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac91a4/32.png) [@deepFritz](https://www.ruby-forum.com/u/deepFritz)\
**Post date:** [October 23, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/how-to-use-an-existing-db-schema-in-my-rails-app/151627/2 "2008-10-23T17:33:21Z")

</div>

On Oct 23, 10:37 pm, deepFritz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> – WHAT if we had ALREADY designed a DB-Schema & even made the Tables,  
> Cheers,  
> I’d try to refactor the schema (assuming this is permissible) to fit  
> the ActiveRecord conventions which I think are reasonable - the big  
> thing being the synthetic id’s and foreign id’s and also the table  
> names.  
> If I can’t rename the tables, I might try to use the set\_table\_name  
> directive (  
> [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001408)  
> ).  
> If the table was using a data field for its primary key, or a  
> composite, i’d add in an ‘id’ integer field and make it the pk, and  
> apply unique not null to the old primary key; I’d then populate id  
> with incrementing integers. Set up a sequence or autoincrement option  
> for this id column (I might have to check that AR is happy with this;  
> maybe I have to tell it what sequence to use using set\_sequence\_name -  
> depending on the db).  
> Alternatively AR also has a set\_primary\_key; if the existing pk was  
> already an incrementing number, I might try this instead. See same  
> url above and look at the other api options there.

For assocations, if the old pk/fk’s were not based on incrementing  
numbers, I’d set up the foreign key in a similar way to the ‘id’  
fields using the rails naming conventions and try to update them by  
using the old primary and foreign keys to basically bootstrap the new  
system - my sql is a little rusty, but you could probably do an UPDATE  
or script it if you had to. I’d probably also have to qualify the  
assocations made in the models using options like :class\_name  
and :foreign\_key etc- see the AR rdocs on assocations.

ActiveRecord is the big issue. Your controllers can do what they  
like.

–  
Daniel B.
