# Existing databases

**URL:** <https://www.ruby-forum.com/t/existing-databases/134224>\
**Category:** Rails\
**Created:** [April 9, 2008, 3:26am UTC](https://www.ruby-forum.com/t/existing-databases/134224 "2008-04-09T03:26:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [April 9, 2008, 3:26am UTC](https://www.ruby-forum.com/t/existing-databases/134224/1 "2008-04-09T03:26:43Z")

</div>

I’m just now getting around to trying out Rails 2. I used Rails 1  
briefly. Back then I was able to start with an existing database that  
followed ActiveRecord conventions, run a few simple commands, and get  
a basic web app. up and running. It seems that has changed in Rails 2.

Am I required to describe the columns of my tables for Rails 2 and/or  
modify generated code? I really want Rails to get information about  
the columns from my database schema and generate starting code for me.  
If I have to describe the tables myself, can someone point me at some  
documentation that describes the steps?

–  
R. Mark V.  
Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [April 9, 2008, 3:34am UTC](https://www.ruby-forum.com/t/existing-databases/134224/2 "2008-04-09T03:34:20Z")

</div>

Rails 2 does everything rails 1.2 does and more.  
It does use SQLite3 however and you still have to set up migrations  
and database relations in your model.  
maybe read activerecord in the API… not quite sure what you’re  
asking.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [April 9, 2008, 3:47am UTC](https://www.ruby-forum.com/t/existing-databases/134224/3 "2008-04-09T03:47:51Z")

</div>

On Tue, Apr 8, 2008 at 8:33 PM, edberner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rails 2 does everything rails 1.2 does and more.  
> It does use SQLite3 however

I noticed that, so I ran “rails -dmysql addressbook” to create my  
addressbook application.

> and you still have to set up migrations  
> and database relations in your model.

I understand the part about setting up database relations in my model  
classes, but I didn’t used to have to set up migrations. Is there a  
way to generate the first migration from existing database tables?  
Maybe this is the key part I’m missing.

> maybe read activerecord in the API… not quite sure what you’re  
> asking.

I know pretty much about ActiveRecord. I have lots of Ruby code that  
is correctly accessing and modifying my database using ActiveRecord  
without using Rails outside the context of a web app. Now I’m trying  
to use that same database with Rails.

> > documentation that describes the steps?  
> > –  
> > R. Mark V.  
> > Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [April 9, 2008, 5:48am UTC](https://www.ruby-forum.com/t/existing-databases/134224/4 "2008-04-09T05:48:23Z")

</div>

Hi Mark,

A migration is a change to the structure of a database, if you already  
have a database structure and you just want to access it in rails you  
don’t need a migration.

Try: ruby script/generate model --skip-migration SomeExistingTable

Note that rails makes assumptions about the tables it uses to store  
model data. You will need to override some of these assumptions if  
your DB schema isn’t what rails is after. For example, see:  
set\_primary\_key ([http://localhost/rails-2.0.2-api/classes/ActiveRecord/](http://localhost/rails-2.0.2-api/classes/ActiveRecord/)  
Base.html#M001400), set\_table\_name ([http://localhost/rails-2.0.2-api/](http://localhost/rails-2.0.2-api/)  
classes/ActiveRecord/Base.html#M001399).

- Barney

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [April 9, 2008, 1:04pm UTC](https://www.ruby-forum.com/t/existing-databases/134224/5 "2008-04-09T13:04:22Z")

</div>

On Tue, Apr 8, 2008 at 10:47 PM, Barney [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Mark,
> 
> A migration is a change to the structure of a database, if you already  
> have a database structure and you just want to access it in rails you  
> don’t need a migration.
> 
> Try: ruby script/generate model --skip-migration SomeExistingTable

For anyone else interested in trying this, use two dashes before  
skip-migration.

However, this still doesn’t work. The generated model .rb files and  
view .erb files still don’t know about the columns in my tables. I’m  
looking for a way to get those generated by automatically obtaining  
information about the columns from my database rather than having to  
explicitly tell Rails about the columns. Is that possible? It was in  
earlier versions of Rails.

> > > and database relations in your model.  
> > > is correctly accessing and modifying my database using ActiveRecord
> > > 
> > > > modify generated code? I really want Rails to get information about  
> > > > the columns from my database schema and generate starting code for me.  
> > > > If I have to describe the tables myself, can someone point me at some  
> > > > documentation that describes the steps?

–  
R. Mark V.  
Object Computing, Inc.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [April 9, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/existing-databases/134224/6 "2008-04-09T13:16:15Z")

</div>

On 9 Apr 2008, at 12:03, Mark V. wrote:

> > Try: ruby script/generate model --skip-migration SomeExistingTable
> 
> For anyone else interested in trying this, use two dashes before  
> skip-migration.
> 
> However, this still doesn’t work. The generated model .rb files and  
> view .erb files still don’t know about the columns in my tables. I’m  
> looking for a way to get those generated by automatically obtaining  
> information about the columns from my database rather than having to  
> explicitly tell Rails about the columns. Is that possible? It was in  
> earlier versions of Rails.

You might want to have a look at the dynamic\_scaffolding plugin, which  
was extracted from rails for 2.0  
that will only fiddle with the view files - nothing will happen to the  
model (but it’s never done anything there)

Fred

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [April 9, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/existing-databases/134224/7 "2008-04-09T13:22:16Z")

</div>

On Wed, Apr 9, 2008 at 6:15 AM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > don’t need a migration.  
> > > explicitly tell Rails about the columns. Is that possible? It was in  
> > > earlier versions of Rails.
> 
> You might want to have a look at the dynamic\_scaffolding plugin, which was  
> extracted from rails for 2.0  
> that will only fiddle with the view files - nothing will happen to the  
> model (but it’s never done anything there)

Thanks! I’ll do that.

I understand that many people feel that dynamic scaffolding is only  
useful for demos, not real web apps. I think what I really want is  
code generation for models. I want my models and views to be generated  
with content related to the fields in my tables. Is that supported out  
of the box by Rails 2?

–  
R. Mark V.  
Object Computing, Inc.
