Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well, Django has an ORM that makes this kind of feature necessary in the first place? ORM's are trying to hide the relational nature of your db, don't they?


The Django ORM is a quite thin abstraction over SQL. But yes, the ORM makes it easy to create this problem by mistake. But sometimes you really don't care. You can write one-off scripts directly in a shell and then it often doesn't matter. I take advantage of this a lot.


Well no, it's in the name - Object Relational Mapping.


Not really... At least, not a good ORM.

The actual idea behind a model based ORM system is to basically solve two problems.

The first is SQL is old and its methodology doesn't directly map onto modern programming paradigms. This one is just apparent by seeing what you get back from raw queries; tables. Want to do something with the table? Go and iterate over the rows individually. Want to get something from a foreign table? Go and do a JOIN before you even start iterating. Want to use binary json properly (the one SQL can actually index so it's not slow as molasses)? Enjoy a serialization syntax that makes you want to rip your eyes out once you get into the fine details. And that's without delving into how every "flavor" of SQL is subtly different with its own idiosyncrasies compared to everyone else. SQLite is not MySQL is not SQL server is not Postgres. Just about the only thing actually shared between them is the extremely basic CRUD tasks (and only if your definition doesn't include any complex WHERE operations). Anything to do with configuring the database is implementation specific in syntax.

ORMs broadly solve this by letting you use OOP paradigms instead; you designate a model that inherits from some base class with fields that themselves are typed to something in the database (including foreign key fields, so the relational status of your database is preserved). After that you can use traditional OOP methods and queries to read and write to the database. Most ORMs will also offer you some form to speed up any query slowdown that might occur due to unnecessary joins by letting you express them beforehand in a syntax that actually makes programmatic sense instead of trying to awkwardly map things.

The other thing ORMs solve is that by default you uh... don't check in your database scheme into a VCS. Some projects just do an SQL dump and expect you to import that. ORMs are basically a solution to that, in that they are a reference for how your database is expected to look like to be functional.

The really fancy ORMs also typically introduce (or support) a migration system so you can also track and easily update your database when your models get changed (which is well, necessary due to the disconnect between the database and the application), but that's not everywhere (SQLAlchemy for example doesn't support it).

To be clear, ActiveRecord is kinda garbage. It does have a migration system but rather than doing model agnostic queries it just references the original models, which leads to problems when setting up the database in development mode since you can't just run all migrations. Instead rake gives you the option to create a database dump of a working environment and have that be the new "canonical" version of the database. For that reason alone it's a mess. It's also incredibly slow, has (as the post mentions) lots of footguns that you need to be careful with and in some cases cannot actually be worked around easily.

ActiveRecord is arguably what killed Ruby for most webapps and I can say that pretty confidently.


> It does have a migration system but rather than doing model agnostic queries it just references the original models, which leads to problems when setting up the database in development mode since you can't just run all migrations.

That's true and this has caused problems before, but there are at least two potential solutions that I've used (none of which are typically referenced in tutorials etc. of course):

1. Write your migration in SQL (ActiveRecord allows that - and in some cases it's even necessary)

2. Copy your model classes into your migration file, namespace them and reference those namespaced models in your migration.

> ActiveRecord is arguably what killed Ruby for most webapps and I can say that pretty confidently.

I don't particularly like ActiveRecord, but do you have any particular evidence for this assertion?


> The first is SQL is old and its methodology doesn't directly map onto modern programming paradigms.

Well, SQL is clunky. But relational ideas map very well with (some) modern programming.

Of course, that's for the kind of modern programming that's not OOP.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: