# Creatulator: Before I go and make it myself, does this exist

**URL:** <https://www.ruby-forum.com/t/creatulator-before-i-go-and-make-it-myself-does-this-exist/78371>\
**Category:** Rails\
**Created:** [March 8, 2006, 11:57am UTC](https://www.ruby-forum.com/t/creatulator-before-i-go-and-make-it-myself-does-this-exist/78371 "2006-03-08T11:57:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Mason](https://www.ruby-forum.com/u/Mason)\
**Post date:** [March 8, 2006, 11:57am UTC](https://www.ruby-forum.com/t/creatulator-before-i-go-and-make-it-myself-does-this-exist/78371/1 "2006-03-08T11:57:14Z")

</div>

Basically, on thing that annoys me to no end is that I am constantly  
having to add things to my model that could just be reflected out by  
the active record. I use PortGres exclusivly, and I know that there  
are ways to ask the database things like, “what is the primary key”  
“what relation does this foreign key belong to” etc…

So what I am about to go and write myself is a database introspector  
that will look at a database and create all the Models for every table  
for me, including all the set\_primary\_key, all the has\_many  
belong\_tos, and many, if not all, of the validates\_blah based on the  
database constraints.

This is going to be quite an undertaking, so before I go and do it,  
does anyone know of something that already does this that has been  
previously written?

---

<div class="post-metadata">

**Author:** ![Mason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Mason](https://www.ruby-forum.com/u/Mason)\
**Post date:** [March 8, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/creatulator-before-i-go-and-make-it-myself-does-this-exist/78371/2 "2006-03-08T18:56:21Z")

</div>

Mason [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Basically, on thing that annoys me to no end is that I am constantly  
> having to add things to my model that could just be reflected out by  
> the active record. I use PortGres exclusivly, and I know that there  
> are ways to ask the database things like, “what is the primary key”  
> “what relation does this foreign key belong to” etc…

ActiveRecord extensively uses introspection. There are already methods  
built  
for finding out all these. For any model call the method ‘Methods’ and  
it will  
return you the list of methods. Try them and find out whether what you  
need is  
already there or not.

–  
Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| Great wits are sure to madness near allied,  
| And thin partitions do their bounds divide.  
|  
| (John Dryden, Absalom and Achitophel, 1681)  
`----

---

<div class="post-metadata">

**Author:** ![Mason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Mason](https://www.ruby-forum.com/u/Mason)\
**Post date:** [March 9, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/creatulator-before-i-go-and-make-it-myself-does-this-exist/78371/3 "2006-03-09T16:58:17Z")

</div>

Mason wrote:

> This is going to be quite an undertaking, so before I go and do it,  
> does anyone know of something that already does this that has been  
> previously written?

Don’t bother. Forget about SQL and the database completely (well almost  
completely) and switch to Rails migrations. Then you can build your  
schema in Ruby and let rails do the primary key stuff in the background.

To Rails the database is just a fancy filestore that we have to use  
because the filesystems haven’t evolved as much as we’d like and object  
databases are mostly crap. Constraints should be implemented in your  
object model, and validated via your test suite. Let the database just  
serve data and deal with the concurrency issue.

It’s a mental paradigm shift - stop checking things in two different  
places.

NeilW

---

<div class="post-metadata">

**Author:** ![Mason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Mason](https://www.ruby-forum.com/u/Mason)\
**Post date:** [March 9, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/creatulator-before-i-go-and-make-it-myself-does-this-exist/78371/4 "2006-03-09T18:04:40Z")

</div>

On 3/9/06, Neil W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mason wrote:
> 
> > This is going to be quite an undertaking, so before I go and do it,  
> > does anyone know of something that already does this that has been  
> > previously written?
> 
> Don’t bother. Forget about SQL and the database completely (well almost  
> completely) and switch to Rails migrations. Then you can build your  
> schema in Ruby and let rails do the primary key stuff in the background.

OK, one my Rails app works with legacy PostgreSQL database (also  
accessed from non-Rails environments), there are views, stored  
procedures, schemas, composite keys, explicitly defined foreign keys  
with CASCADE/RESTRICT options, non-serial primary keys (varchars),  
indexes, CHECK and DEFAULT constraints for columns; triggers; domain  
types.

Rails migrations, with their idea of logic in application only and  
“least common denominator” support for databases just can’t handle all  
that and aren’t that useful for me, so I stick with pure SQL  
incremental updates.

---

<div class="post-metadata">

**Author:** ![Mason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Mason](https://www.ruby-forum.com/u/Mason)\
**Post date:** [March 9, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/creatulator-before-i-go-and-make-it-myself-does-this-exist/78371/5 "2006-03-09T18:14:03Z")

</div>

On 3/8/06, Mason [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So what I am about to go and write myself is a database introspector  
> that will look at a database and create all the Models for every table  
> for me, including all the set\_primary\_key, all the has\_many  
> belong\_tos, and many, if not all, of the validates\_blah based on the  
> database constraints.

Regarding associations, yes, it is possible to generate models  
automatically with set\_primary\_key, has\_many and has\_one declarations,  
but I suspect that manual tweaking still will be needed, e.g. when you  
want to change name of the association, or use class names that don’t  
correspond literally to names of database tables.

And what about refreshing so generated models? Schema namespace support?

And about validations: it is possible to define one universal  
validate\_against\_db\_constraints method, that would check all CHECK and  
FOREIGN KEY constraint using single query to database and providing  
rich feedback - what constraints are broken and which columns break  
them 🙂
