53 points · by rohitarondekar · 15y ago · 34 comments
Personally, I prefer the Rails way of doing it, since it keeps my model files cleaner and avoids ambiguity (to me) when adding/removing/changing attributes. Of course, there is a tradeoff in increased startup time because Rails has to analyze your tables, but PassengerPreStart pretty much takes care of that annoyance for me (and I use background processing that doesn't require the Rails environment be instantiated for every job).
I've written about this before, but here's the short version:
1) The database holds the authoritative schema. You shouldn't duplicate it in your code.
2) What if you want to do something more complicated, like split the values in one column into two columns? You're going to need support for manual migrations anyway.
3) Including column declarations in your model forces the database's strong typing system onto what should be plain old Ruby/Python objects. The result is ineffective domain objects.
> 1) The database holds the authoritative schema. You shouldn't duplicate it in your code.
I care about the authoritative data model, which is slightly different from the authoritative schema. Beyond that, I don't edit the database schema. There are many cases where I'm happier to have a complete data model in one location versus (at least) two and needing to mentally parse how rails reacts to the database schema.
> 2) What if you want to do something more complicated, like split the values in one column into two columns? You're going to need support for manual migrations anyway.
In case you didn't notice, the automatic migration was happening through a --auto command line flag. Manual migrations exist, and are the default.
> 3) Including column declarations in your model forces the database's strong typing system onto what should be plain old Ruby/Python objects. The result is ineffective domain objects.
Why should these be plain old Ruby/Python objects? ActiveRecord already is a pretty extensive DSL for relating objects to database tables. Does including attribute descriptions really make it "not a plain old Ruby object" when we already have well developed support for field validation and relationship declarations?
That said, I don't think the auto migrations are good idea. You're right, eventually you'll want to do a manual migration. At that point it's painfully easy to create a situation where your manual migration depends on properties of your model that have since changed. It's a problem akin to using your AR classes to help manipulate data in a migration. We have a point in our migration history we simply cannot restart from because it uses a model class that has since been removed.
That's nonsense, your application implies the authoritative schema as it relies on a given set of model fields to exist. More importantly any given version of your application may imply a different schema.
Artificially detaching the schema from the code only causes problems and adds pointless complexity to the overwhelming majority of applications (which don't share the database with anything else).
Having to reach for irb or psql only to figure out what fields exist on a given model is backwards. Having to manually craft migrations with proper indexes, keys and constraints to match the separately defined validations and associations is completely ass-backwards.
I know that i'm probably duplicating code, but with this way, i don't have to look at the database to know which properties a model has, i just have to go to the .py file.
Also, i can use migrations as well to make sure the database is with the right version.
Why?
> You shouldn't duplicate it in your code.
If the database does not hold the authoritative schema, you're not duplicating anything.
> What if you want to do something more complicated, like split the values in one column into two columns? You're going to need support for manual migrations anyway.
Of course, but it's not like the generated migrations are special. They're just written for you by the migration tool. One thing I'm sure you'll agree with is that computers are good at grunt, non-creative work. If the computer can write a migration, why not let it do so?
> Including column declarations in your model forces the database's strong typing system onto what should be plain old Ruby/Python objects. The result is ineffective domain objects.
Hogwash.
http://datamapper.org/ https://github.com/datamapper/dm-rails
Datamapper was one of the great parts of the Merb framework, and with rails 3 you can use it in place of Activerecord.