# Base migration file

**URL:** <https://www.ruby-forum.com/t/base-migration-file/111397>\
**Category:** Rails\
**Created:** [August 6, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/base-migration-file/111397 "2007-08-06T22:14:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [August 6, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/base-migration-file/111397/1 "2007-08-06T22:14:19Z")

</div>

I’m a total noob to RoR so I’ve started my app from scratch countless  
times. As I figure out what I need in my DB I’ve been adding it to a  
file 001\_build.rb - so when i restart AGAIN i just run the migration  
and i’m back up and running rather quickly. Is this a BAD idea?

It hasn’t been an issue until today when I ran my first  
scaffold\_resource - which ran fine until i pulled it up in the browser  
and it had no clue that my db was already up and running with all the  
tables and fields I needed.

I can run the scaffold\_resource with all the field names and types,  
but i have NO clue how to force the use of a foreign key like i’ve  
been using in my migration file. This is what I’ve been using:

execute “alter table pages add constraint fk\_pages\_families  
foreign key (family\_id) references family(id)”

So, if i could figure out how to do the above in my scaffold\_resource  
command I would be all set.

Anyone have some insight for me? Thank you for your consideration and  
time!

---

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [August 7, 2007, 12:53am UTC](https://www.ruby-forum.com/t/base-migration-file/111397/2 "2007-08-07T00:53:33Z")

</div>

On Aug 6, 2007, at 15:13 , Kedron wrote:

> I’m a total noob to RoR so I’ve started my app from scratch countless  
> times. As I figure out what I need in my DB I’ve been adding it to a  
> file 001\_build.rb - so when i restart AGAIN i just run the migration  
> and i’m back up and running rather quickly. Is this a BAD idea?

Migrations are intended to be cumulative, so there’s really no reason  
to keep everything in the first migration file. When you run rake  
db:migrate, all of the necessary migrations are run to bring the  
database up to date. One reason _not_ to put it all in one large file  
is that it makes properly defining the down method (for undoing a  
migration) more complicated.

> I can run the scaffold\_resource with all the field names and types,  
> but i have NO clue how to force the use of a foreign key like i’ve  
> been using in my migration file. This is what I’ve been using:
> 
> execute “alter table pages add constraint fk\_pages\_families  
> foreign key (family\_id) references family(id)”
> 
> So, if i could figure out how to do the above in my scaffold\_resource  
> command I would be all set.

I always excecute SQL in my migrations rather than using the Ruby-  
language migration SQL DSL, so I’ll leave this to someone else.

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![Kedron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@Kedron](https://www.ruby-forum.com/u/Kedron)\
**Post date:** [August 7, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/base-migration-file/111397/3 "2007-08-07T14:26:54Z")

</div>

Thanks for the insight Michael! I can see how the down method could  
help down the road! I’ll quit using the one LARGE migration file.

On that note, does anyone know how to require a foreign key in the  
generate command? I currently have it in the rake file, but I don’t  
know how to do the same thing from the command line.

Thank you.
