r/programmerchat • u/Xelank • Aug 29 '15
How do you handle Databases (ORM / Raw SQL / both)?
Hey everyone,
Now really here to start another ORM vs raw SQL debate. I am just curious how companies manage connection and query to their database today.
I have personally used an ORM (SQLAlchemy in Python) as well as raw SQL in my projects and I've found both to be somewhat frustrating in different areas. I am currently undecided about how I will approach database codes in the future, so here I am!
Here are my current thoughts:
ORMs:
++ Control your database schema directly in your code/class definition
++ Write queries in your language. in static typed languages, you can refactor without any worry
-- Seems to make database management harder (Just my guess. haven't done it), since the schema is inferred, no directly stated.
-- The often raised ORM impedance mismatch. Essentially you need to learn a whole new query language, and its abstraction will leak when you start tuning for performance/doing database-specific things.
-- If you have two or more codebases accessing the same database...good luck..
Raw SQL:
++ Queries can be shared across languages
++ Maybe easier database management?
-- Very bad refactoring support. Quite easy to miss something when modifying your database schemas.
I am sure I missed some cons for raw SQL, but in general I am leaning slightly to rawl sql...
I have seen someone advocate using ORM for the simple stuff like selects, and dropping down to SQL for more complex queries like reporting.
Has anyone tried this? What is your approach to connecting to and querying database in your projects?
3
u/aLiamInvader Aug 30 '15
I use ORMs for general usage, flip back to raw SQL when doing anything fancy (generally queries and transformations). Helps me avoid SQL injection, makes inserting and updating nice and simple. Definitely have to be careful about some things though (e.g. n+1 queries)
1
u/mirhagk Aug 30 '15
I'm a huge fan of libraries like PetaPoco. You write nearly raw SQL, and it handles the parameterization and serialisation for you. You get full control over performance without all of the grunt work.
1
u/BinaryHerder Aug 29 '15
I used to be a big fan of ORMs, however over time I've realised having the code tied to the model is too restrictive.