That’s a question I ask myself after reading an article about an alternative approach to realize migrations in Rails. With that approach, you define the columns of your table in the model and then you generate the migration files from the model. You no longer have to touch any SQL scripts or to write migration files — if there are changes in the table definition, you make them in the model.
I like this idea, as it allows you to work on a higher level, i.e. you no longer have to deal with foreign keys, association tables etc. That’s automatically done for you. And the more I think about this topic, the more I am convinced that the table definition should be in the model. You can look at the column types of a table as basic “validation rules” used by the database to check whether the data you want to insert are correct. For example, a column type of varchar(255) could be translated to the validation rule “maxLength = 255”. By moving those “validation rules” to the model level, we have all validation rules related to a model in one place. And that’s the logical place where they should be in my opinion.
What do you think about this idea?