# Database independent representation of schema

**URL:** <https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966>\
**Category:** Rails\
**Created:** [December 4, 2005, 3:11am UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966 "2005-12-04T03:11:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 3:11am UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/1 "2005-12-04T03:11:21Z")

</div>

Guys,

Been away from Rails for awhile but am finally diving back in.

I know there had been some discussion of (and I have been logging for)  
adding definition of a database schema in a file outside of the  
database,  
a la Hibernate.

In other words, in Hibernate you define your object relationships in  
external xml files and you can then use the packaged tools (hbm2ddl) to  
generate DDL for the target database.

Why is this nice? First, you go about designing your application truly  
thinking about the objects, and not the tables, from the get go (to a  
certain extent). But secondly, and most important, I don’t have to  
rewrite  
the DDL each time I switch from say, Hsqldb (for demos), to PostgreSQL  
(production).

Has there been any movement on things in this regard in Rails?

Thanks for the info.

John

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 3:43am UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/2 "2005-12-04T03:43:42Z")

</div>

Hi,

You may want to have a look at migrations that allow you to define it  
in a database agnostic way for postgres and mysql.

[http://wiki.rubyonrails.com/rails/pages/UnderstandingMigrations](http://wiki.rubyonrails.com/rails/pages/UnderstandingMigrations)  
[http://api.rubyonrails.com/classes/ActiveRecord/Migration.html](http://api.rubyonrails.com/classes/ActiveRecord/Migration.html)

If you need foreign key enforcement then you will need to look at  
“Foreign Key Schema Dumper Support” on  
[http://wiki.rubyonrails.com/rails/pages/Plugins](http://wiki.rubyonrails.com/rails/pages/Plugins)

–  
HTHs,

Peter D.

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 5:56pm UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/3 "2005-12-04T17:56:33Z")

</div>

Peter D. said:

> You may want to have a look at migrations that allow you to define it  
> in a database agnostic way for postgres and mysql.

Thanks Peter. This is definitely a step in the right direction. It seems  
somewhat green in terms of features (not yet supporting NOT NULL, etc),  
but definitely workable. Too bad it doesn’t support sqlite…we use it  
for  
demo for our rails app while the production version runs on PostgreSQL.

Thanks for the pointer!

John

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 6:46pm UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/4 "2005-12-04T18:46:18Z")

</div>

I don’t know much about migrations yet but the following two points made  
me  
wonder.

On 12/4/05, John W. wrote:

> It seems somewhat green in terms of features (not yet supporting NOT  
> NULL, etc)

what about “:null =\> false”?

Too bad it doesn’t support sqlite

> From [http://glu.ttono.us/articles/2005/10/27/the-joy-of-migrations](http://glu.ttono.us/articles/2005/10/27/the-joy-of-migrations)

“Migrations are database agnostic. What this means is that you can write  
your description of the database in ruby -once- and have it easily  
translate  
to MySQL, PostgreSQL or SQLite out of the box.”

-Peter

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 7:39pm UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/5 "2005-12-04T19:39:09Z")

</div>

Currently I use raw SQL for DDL because I can also populate/seed my  
database  
in the same file. It is nice because my INSERT statements can be written  
directly below my CREATE TABLE statements.

If I switch to using schema.rb to define my database what is the best  
way to  
seed it?. I’d like to type one command to both define and seed the  
database.

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 4:40am UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/6 "2005-12-04T04:40:17Z")

</div>

On 12/3/05, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> generate DDL for the target database.
> 
> Why is this nice? First, you go about designing your application truly  
> thinking about the objects, and not the tables, from the get go (to a  
> certain extent). But secondly, and most important, I don’t have to rewrite  
> the DDL each time I switch from say, Hsqldb (for demos), to PostgreSQL  
> (production).
> 
> Has there been any movement on things in this regard in Rails?

You’re looking for migrations and schemas. You still define your  
relationships and all that in the code. Schemas and migrations use  
the database agnostic methods of the Connection Adapter for the DDL  
queries. It makes it a snap to develop and test in different  
databases, support multiple ones, and make iterative changes.

Kevin wrote a great article on Migrations themselves. The actual  
methods are the same for schemas. Just type rake db\_schema\_dump to  
see the autogenerated schema for your current database.

[http://glu.ttono.us/articles/2005/10/27/the-joy-of-migrations](http://glu.ttono.us/articles/2005/10/27/the-joy-of-migrations)

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 11:07pm UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/7 "2005-12-04T23:07:55Z")

</div>

Hi,

On 12/5/05, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Currently I use raw SQL for DDL because I can also populate/seed my database  
> in the same file. It is nice because my INSERT statements can be written  
> directly below my CREATE TABLE statements.
> 
> If I switch to using schema.rb to define my database what is the best way  
> to seed it?. I’d like to type one command to both define and seed the  
> database.

You can populate your database using the model object directly. ie

MyFoo.create( :field1 =\> ‘blah’, :field2 =\> 2 )

or

foo = MyFoo.new  
foo.field1 = ‘blah’  
foo.field2 = 2  
foo.save

–  
Cheers,

Peter D.

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [December 4, 2005, 10:42pm UTC](https://www.ruby-forum.com/t/database-independent-representation-of-schema/51966/8 "2005-12-04T22:42:59Z")

</div>

On Dec 4, 2005, at 10:35 AM, Peter M. wrote:

> Currently I use raw SQL for DDL because I can also populate/seed my  
> database in the same file. It is nice because my INSERT statements  
> can be written directly below my CREATE TABLE statements.
> 
> If I switch to using schema.rb to define my database what is the  
> best way to seed it?. I’d like to type one command to both define  
> and seed the database.

See the second example in the [ActiveRecord::Migration documentation]  
[1](http://api.rubyonrails.com/classes/ActiveRecord/Migration.html).

–Steve
