4 ms·
I'm working at three projects with three languages and three different ORMs so you have my sympathy. They're the ORMs of Django, Rails, Phoenix. Rails' got the
by pmontra 7y ago
I'm working at three projects with three languages and three different ORMs so you have my sympathy. They're the ORMs of Django, Rails, Phoenix. Rails' got the easiest ORM to use by far, maybe because IMHO it's now the closest to SQL. Phoenix's Ecto is needlessly complicated and Django's is as verbose as Python's libraries can get. Example: Model.objects.get(), because nobody could understand Model.get(), right? /s
So, along the years I felt sometimes like I have to relearn how to do SQL in arbitrary library X, for the sake of it.
However ORMs have an advantage: no need to change queries when adding/removing fields to the database. No need to deserialize data from resultsets. The usually migrate the database either by applying migrations to the database and infer models (Rails) or syncing the db with the model (Django). Weirdly Ecto forces the developer to both write the migration and the model.
My ideal ORM would be something that lets me write
sql("select * from employee join department
on employee.department_id = department.id
order by employee.id desc limit 10").each do |record|
puts("#{record.employee.id}, #{record.department.name}")
I don't know how that would play with static typed languages but if all ORMs would be like that we could know only SQL and be able to perform arbitrarily complex queries.
- status_quo69 7y agoSounds like you don't want an ORM but a DB driver like psycopg2 http://initd.org/psycopg/docs/cursor.html#fetch http://initd.org/psycopg/docs/cursor.html#fetch
- pmontra 7y agoI know psycopg2 and other drivers for several languages. They're ok to make queries but they lack everything else ORMs are good at. I want an ORM, but a smarter one and as similar as possible across languages. Instead everybody reinvented the wheel in every language, with different approaches, more or less over engineering their solution and making the polyglot experience as difficult as learning English and Russian instead of Italian, French, Portuguese, Spanish (learn one, you'll get the others quickly.) My background is that I learned SQL almost 10 years before the first Java ORMs went mainstream. I've been using ORMs since the 90s but for writing queries they're a kind of waste: the query in the example in Rails is Employee.joins(:departments). limit(10). order_by("employees.id desc") at the cost of 1) defining some relationships between the tables in the models 2) learning how to translate SQL in Ruby ActiveRecord is magically convenient most of the time. Sometimes it's quicker to write that SQL, sometimes is not. Django (I can't remember the name of the ORM) and Ecto are nearly always slower to write than SQL, especially Ecto. For complex queries SQL is the only way to go in any framework and the ORM is there only to deserialize the results, so why not using it for simple ones too? Employee.find(params["id"]) is about the limit.
- slig 7y ago> Example: Model.objects.get(), because nobody could understand Model.get(), right? /s That has a simple explanation: each Model can have multiple Managers [1], and the `objects` is the default one. Also, by convention, methods on the Model are usually relative to "one DB record", and methods on the manager are relative to N records. [1]: https://docs.djangoproject.com/en/2.2/topics/db/managers/ https://docs.djangoproject.com/en/2.2/topics/db/managers/
- pmontra 7y agoThank you, I know now why Django inflicts that .objects method to everybody :-) I'd rather design a less verbose API for the default manager Model.get() Model.filter() ... and Model.manager.get() if one really needs custom managers. The fact that I didn't know about managers after years of Django (and nobody told me) should be telling of me and my team, of course, but also how needed managers are. Having to go through that API without shortcuts is not nice to developers.
- slig 7y agoYou're welcome. I'm probably biased because Django has been my framework of choice for more than 10 years, but I really like the distinction between methods relative to the Model and methods that act on multiple records and the `objects` thing doesn't bother me.