Rails generating model migrations

I read about Rails Migrations to help me get started creating a rails project.

I am a bit confused about file generation in db / migrate.

As I developed my application, I start with models ... outlining all the objects that I will have in the system as much as possible. I would like to automatically generate migration files from these models using a rail migration generator.

Yes, I know that "creating a manual migration is easy." And I know that I can do it manually, but I don’t understand why the tool is separated from pre-created models.

Based on my understanding of the article and other issues of migration to SO, I can generate the migration as follows:

rails generate migration SomeObj field:string some_other_field:integer 

What I am not getting is why I need to pass to fields when my model already exists for SomeObj? Rails failed to detect it from some_obj.rb and create a migration from there?

Also, when I have a more complex model with has_many, belongs_to and has_to_and_belongs_to_many relationships, it would be nice if it automatically creates JOIN tables and fields with the correct names (for example, foreign_obj_id, foreign_obj_ids)

In the previous project, I did not have to deal with migrations because I used Mongo + Mongoid - and collections were automatically generated due to the nature of Mongo's work (collections that do not exist are automatically created when inserted or updated). Now I'm using Postgres with this Rails application (but I'm sure the same thing will happen with MySQL or any other relational database).

In any case, is there no assistant in this regard? Do I need to manually create all these migrations?

+4
source share
1 answer

You have it in the opposite direction. First you need to build your migrations, and your models are the second. Migrations promote the state of the database. Your models reflect the current state of the database. There is no 1-to-1 mapping for migration.

I would like to automatically generate migration files from these models using a rail migration generator.

Unable to execute.

You use rails generate migration to create a migration that, almost as a side effect, creates a stub model file. You cannot use an existing model file to create a model migration because the model does not contain any information about the actual columns that make up the model. Rails will have to extract and imply all the necessary information from your associations / validations, and even then this will lead to incorrect processing of most columns.

Also, when I have a more complex model, ... it would be really nice if it automatically creates JOIN tables and fields with the correct names

Again, you cannot. You are responsible for defining the database schema, and the way you do this is to create migrations. You can do this manually or through rails generate , but the process you must follow first creates your migrations and your models second.

As an example, here is a complete model, ready for release:

 class MyModel < ActiveRecord::Base end 

This model definition can wrap a table containing 10 columns or 1000 columns; you (and Rails) absolutely do not know, based on the definition of the model.

Here's another model containing at least one column, but again, you don't know which column:

 class MyModel < ActiveRecord::Base validates :code, presence: true, uniqueness: true end 

Is code string or a number? Should there be an index in the column? There is no way to find out.

+10
source

All Articles