Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Generating Migrations From Model

I've been reading about Rails Migrations to help me start building a rails project.

I'm a bit confused on the generation of the files in db/migrate.

The way I've been designing my application is by starting with the models... outlining all of the objects that I'm going to have in the system as best I can. I would like to generate the migration files FROM these models automatically with the rails migration generator.

Yes, I know "creating migrations manually is easy." And I know I could do it manually, but what I don't understand is why the tool is separated from the pre-created models.

Based on my understanding from the article and other migration questions on SO, I can generate a migration like so:

rails generate migration SomeObj field:string some_other_field:integer

What I don't get is why I need to pass in the fields when my model already exists for SomeObj? Couldn't Rails detect it from the some_obj.rb and create the 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 really nice if it autocreated the JOIN tables and fields with the correct names (e.g. foreign_obj_id, foreign_obj_ids)

In a previous project I didn't have to deal with migrations because I used Mongo+Mongoid - and collections were automatically generated due to the nature of how Mongo works (collections that doesn't exist get automatically created upon inserting or updating). Now with this Rails app I'm using Postgres (but I'm sure the same thing would happen with MySQL or any other relational database).

Anyway, is there no helper for this kind of thing? Do I have to create all these migrations manually?

like image 581
K2xL Avatar asked Dec 08 '22 18:12

K2xL


1 Answers

You've got this backwards. You need to build your migrations first, and your models second. Migrations advance the state of the database. Your models reflect the current state of the database. There is not a 1-to-1 mapping of models to migrations.

I would like to generate the migration files FROM these models automatically with the rails migration generator.

Can't be done.

You use rails generate migration to generate the migration, which, almost as a side-effect, generates a stub model file. You cannot use your existing model file to generate the model's migration, because the model doesn't contain any information about the actual columns that make up the model. Rails would have to extract and imply all the necessary information from your associations/validations, and even then it would get most of the columns wrong.

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

Again, you can't. You're responsible for defining your database schema, and the way you do so is by building migrations. You can do so manually, or via rails generate, but the process you should be following is building your migrations first, and your models second.

By way of example, here is a complete model, ready for production:

class MyModel < ActiveRecord::Base
end

That model definition might wrap a table that contains 10 columns or 1000 columns; you (and Rails) have absolutely no way of knowing based on the model definition.

Here's another model, which contains at least one column, but again, you have no way of knowing what kind of column:

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

Is code a string or a number? Should there be an index on the column? There is absolutely no way of knowing.

like image 198
meagar Avatar answered Dec 11 '22 07:12

meagar