News for hackers. Stuff that matters.
Re:Learn SQL, dammit by zzzeek on Wednesday May 08, 2013 @02:50PM (#5676075)
The main point that one should know SQL as much as possible before using ORMs, I agree with fully. The point that applications should be written by quick-prototyping with an ORM, then replacing the ORM entirely with raw SQL, I could not disagree with more. Since he is using my own ORM (SQLAlchemy) as his example, I'd like to point out (as many of you know I always do) that SQLAlchemy's entire approach is one of exposing the relational model at all times. There is virtually no need to drop out of using the Query and/or select() constructs into raw SQL strings, as these constructs can represent SQL fully and more or less directly, not really any different in concept than when the relational database itself parses an incoming SQL string into a tokenized parse tree internally. The results returned are tuples. If your tuples happen to line up with the attributes in your object model, then you can tell it to get objects back. There is no sharp red line between "I'm using an ORM!" vs. "I'm using SQL!". With a mature tool like SQLAlchemy you're using both, and the tool is there to automate your work with SQL, not to replace it.

So of course, learn SQL as fully as possible. But I recommend using an ORM that allows you to make full use of your SQL knowledge at all times.

Re:Learn SQL, dammit by moron4hire on Wednesday May 08, 2013 @02:55PM (#5676107)
This is very important. Any time you build a bike with training wheels, you need to build in a way to take the training wheels off. Otherwise, at some point the solution is to not make faster training wheels, but discard the bike entirely.
Re:Learn SQL, dammit by derleth on Wednesday May 08, 2013 @03:26PM (#5676282)
Is C training wheels on assembly?
Re:Learn SQL, dammit by coldtea on Wednesday May 08, 2013 @05:46PM (#5677165)
No. But an ORM is training wheels on SQL.
Re:Learn SQL, dammit by moron4hire on Wednesday May 08, 2013 @06:54PM (#5677583)
Right, and (most) C compilers let you fall back on assembly if you absolutely must. So my point still holds, even if you do something silly like employ hyperbole on it.
Re:Learn SQL, dammit by aidos on Wednesday May 08, 2013 @03:07PM (#5676186)
Indeed. I do worry about the level of SQL knowledge that developers have in general these days. Without understanding it - and the relational model (which I'm a big fan of) - it's impossible to optimise the way you use the ORM.

OT but thanks for SQLAlchemy. It's the model ORM in my opinion. It's the only one I've ever used that feels like it respects SQL. When I started working with Python fulltime I spent days reading through the SQLAlchemy source - I learnt a lot from that.

Re:Learn SQL, dammit by jfb on Wednesday May 08, 2013 @03:50PM (#5676435)
I have no interest in the 'O' or 'M' in an ORM, but I am very interested in a language better than SQL for interacting with SQL-based pseudo-relational datastores. I've found SQLAlchemy to be a pretty good tool in this respect.
Re:Learn SQL, dammit by tjr on Wednesday May 08, 2013 @02:59PM (#5676133)
If you've already learned SQL and are comfortable using it directly, do you think there is any reason that it'd be wrong to continue doing so?
Re:Learn SQL, dammit by malkia on Wednesday May 08, 2013 @03:45PM (#5676409)
Other people on your team, might not be on the same SQL level as you. E.g. you can create very fast update/bulk/etc. operations directly in your used SQL engine, but then provide some ORM for people not familiar so that they can get access (speed might not be critical to them, just access to the db).

An example - in the game industry we often have technical artists - pretty cool bunch of folks with awesome talents - art (maya, motionbuilder, etc.) and coding (python, mel, C/C++) - these folks are never afraid to step into the dark woods, and would take SQL and learn it as if it was nothing dangerous, but they surely won't mind a good ORM. From what I've learned, they see most of these things as tools.

Re:Learn SQL, dammit by jrochkind1 on Wednesday May 08, 2013 @04:05PM (#5676516)
Any reason it would be 'wrong'? There are few things in software engineering that are universally 'right' or 'wrong', it all depends on context.

There are plenty of people who already know SQL and are comfortable using it, but still prefer to use an ORM though. It's not like ORMs are only used or liked by people who don't know SQL.

Re:Learn SQL, dammit by tjr on Wednesday May 08, 2013 @04:40PM (#5676777)
Fair enough. Replace "wrong" with "generally considered a suboptimal idea".
Re:Learn SQL, dammit by jrochkind1 on Wednesday May 08, 2013 @05:59PM (#5677231)
Still, 'it depends'. Yeah, there are all sorts of reasons, depending on context (and plenty of people to argue all those reasons).

Personally, I agree with OP that nobody should ever use an ORM as an excuse to not know SQL. You need to know SQL anyway.

So, once we agree on that, your question "Is there any reason to use an ORM if I know SQL" (that's basically what you mean when you phrase it in the inverse "Is there any reason it would be suboptimal to NOT use an ORM", right?) -- basically just boils down to "Is there ever any reason to use an ORM?".

A topic which is basically the equivalent editor/OS war of db-based web development on HN or reddit. Meaning it's an argument that can and does go on forever, and you can find in-depth treatments of in many other threads.

Re:Learn SQL, dammit by tjr on Thursday May 09, 2013 @10:45AM (#5680676)
Thank you. I believe my question has been answered, even if I failed at expressing it the way I intended.
Re:Learn SQL, dammit by PleaseBeSerious on Wednesday May 08, 2013 @03:06PM (#5676177)
If you already have a layer of abstraction to your SQL statements, then it can be arguable. But if your are littering SQL statements everywhere, making changes to the database can be more tedious.

Also if you want to support more than one type database, I would use an ORM.

Re:Learn SQL, dammit by zzzeek on Wednesday May 08, 2013 @03:00PM (#5676140)
it's really not as much about the querying (though there is a lot of time-saving automation to be had there) as it is about integrating the data in your object model with the tuples being shuttled to/from the database. Like, at what point do you get sick of writing redundant "INSERT INTO <table>" over and over again? Are there really people who still don't see the time-wasting, code-cluttering repetition in that?
Re:Learn SQL, dammit by silverbax88 on Wednesday May 08, 2013 @03:10PM (#5676201)
I'm still amazed that there are developers who would rather spend more time maintaining/debugging/optimizing ORM code in a mature app that just learning a simple INSERT statement.
Re:Learn SQL, dammit by untog on Wednesday May 08, 2013 @03:44PM (#5676404)
who would rather spend more time maintaining/debugging/optimizing ORM code

But that's kind of the point- with a decent ORM library there isn't really much work to do.

    model = MyModel.load(id)
Add a new field to your model? It'll handle it. It might even modify the table for you. With raw SQL you'd have to go and edit your UPDATE and INSERT statements each time. Seems like a lot more maintenance/potential debugging to me.
Re:Learn SQL, dammit by silverbax88 on Wednesday May 08, 2013 @04:04PM (#5676512)
Okay, add a messaging trigger on update and insert between two joined permission tables, without creating a mutating trigger.

SQL is about a lot more than CRUD and tables.

Re:Learn SQL, dammit by untog on Wednesday May 08, 2013 @04:57PM (#5676893)
SQL is rarely about a lot more than CRUD and tables. I would think that over 95% of the SQL code ever written has been CRUD operations.

ORMs exist to simplify CRUD operations. If you are trying to do something that is a not a CRUD operation then you'd be mad to try and use an ORM to do it.

Re:Learn SQL, dammit by zzzeek on Wednesday May 08, 2013 @04:24PM (#5676669)
so add a trigger to your tables. How is an ORM getting in the way of that?
Re:Learn SQL, dammit by d23 on Wednesday May 08, 2013 @03:22PM (#5676265)
Because in my experience, the "maintaining/debugging" step is almost non-existent (isn't that the point of the ORM?), and the "optimizing" step hasn't been an issue for the traffic our sites get.
Re:Learn SQL, dammit by papsosouid on Wednesday May 08, 2013 @03:31PM (#5676313)
> the "maintaining/debugging" step is almost non-existent (isn't that the point of the ORM?)

Nobody can agree on the point of ORMs, which is a large part of the reason why there's so much debate over whether to use them and how. I must say, if their point is to eliminate maintenance and debugging, then they are failing miserably at it.

Re:Learn SQL, dammit by camus on Wednesday May 08, 2013 @03:06PM (#5676172)
> then replacing the ORM entirely with raw SQL,

anyway it is usually never the case. Why would you build a complex app only to tear it appart with going back to raw SQL ?

i'm not talking about SQLAlchemy but often the main problem with ORM is performance. Especially when a orm has its own query language on top of the SQL language ( Like Hibernate or Doctrine ).

A good ORM imho is what you described , something very light but still usefull enough so one doesnt have to do data transformation from rows to objects. How does SQLAchemy fetch related objects ? is it is lazy ? or does it fetch everything when doing a join query for instance.

Anyway thank you for your great work with SQLAlchemy.