Search code examples
ruby-on-railsdatabase-migration

Model/Migration creation order matters in Rails?


I have generated 4 models in rails. Now some of them requires columns for foreign keys from each other.

Suppose I have a table User and other table Post. Now do i need to generate the models in order for ex. User and then Post, so that Posts table could contain user_id column in it?

I am not running rake db:migrate now. What I'm doing is generating models and specifying the columns that might be necessary.

I want to know is, does rake db:migrate takes care of the order automatically and i can create the models in any order? Or as migration files has timestamps attached to it in the file name, it will be processed according to the order of its creation and will give me the error for ex. user_id foreign key dependency, table Users not found?


Solution

  • There are no foreign key dependencies in Rails migrations, so as long as you are not creating dependent data for the models in the migrations, it will work correctly.