DEV Community

Discussion on: ORM vs Pure SQL: Which to Choose?

Collapse
 
roxiroas profile image
Roxi Roas

And can I use both methods in the same project?

Collapse
 
marmariadev profile image
Maria M. • Edited

Yes, if you wish, you can combine ORM and pure SQL in a project to maximize efficiency: use ORM for rapid development and simple maintenance, and pure SQL for complex queries and performance optimization. This gives you the best of both worlds: productivity and detailed control as needed.

Collapse
 
gosuteacher profile image
GosuTeacher

Some ORMs have entity tracking and let's say you update via raw query an entity that's already tracked, your tracking object won't be updated and this cause a lot of confusion and debugging pain. That's why I would avoid combining both.

Collapse
 
marmariadev profile image
Maria M.

I understand your concerns, GosuTeacher. However, many modern ORMs allow for the execution of custom SQL queries while still maintaining entity tracking and management. This offers us flexibility for complex cases without losing the advantages of the ORM. The key is to use this feature wisely, ensuring that complex operations integrate well with the overall data management. This way, we can enjoy the best of both worlds: the simplicity of the ORM and the precision of pure SQL for specific needs.