Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the limitations of Django's ORM? [closed]

Tags:

I've heard developers not wanting to use ORM, but don't know why. What are the shortcomings of the ORM?

like image 359
Kar Avatar asked Feb 02 '12 12:02

Kar


People also ask

What is Django's ORM?

One of the most powerful features of Django is its Object-Relational Mapper (ORM), which enables you to interact with your database, like you would with SQL. In fact, Django's ORM is just a pythonical way to create SQL to query and manipulate your database and get results in a pythonic fashion.

When should you not use an ORM?

Whether or not you should use ORM isn't about other people's values, or even your own. It's about choosing the right technique for your application based on its technical requirements. Use ORM or don't based not on personal values but on what your app needs more: control over data access, or less code to maintain.

Is Django ORM good?

The Django ORM is a very powerful tool, and one of the great attractions of Django. It makes writing simple queries trivial, and does a great job of abstracting away the database layer in your application. And sometimes, you shouldn't use it.

Is Django ORM active record?

Django ORM uses the active record implementation - you'll see this implementation in most ORMs. Basically what it means is that each row in the database is directly mapped to an object in the code and vice versa. ORM frameworks such as Django won't require predefining the schema to use the properties in the code.


2 Answers

Let me start by saying that I fully advocate the use of the ORM for most simple cases. It offers a lot of convenience when working with a very straightforward (relational) data model.

But, since you asked for shortcomings...

From a conceptual point of view, an ORM can never be an effective representation of the underlying data model. It will, at best, be an approximation of your data - and most of the time, this is enough.

The problem is that an ORM will map on a "one class -> one table" basis, which doesn't always work.

If you have a very complex data model - one which, ideally, cannot be properly represented by a single DB table - then you may find that you spend a lot of time fighting against the ORM, rather than having it work for you.

On a practical level, you'll find that there is always a workaround; some developers will be partisan in their support for/against an ORM, but I favour a hybrid approach. The Django works well for this, as you can easily drop into raw SQL as needed. Something like:

Model.objects.raw("SELECT ...")

ORMs take a lot of the work out of the 99.99% of other cases, when you're performing simple CRUD operations against your data.

In my experience, the two best reasons to avoid an ORM altogether are:

  • When you have complex data that is frequently retrieved via multiple joins and aggregations. Often, writing the SQL by hand will be clearer.
  • Performance. ORMs are pretty good at constructing optimised queries, but nothing can compete with writing a nice, efficient piece of SQL.

But, when all's said and done, after working extensively with Django, I can count on one hand the number of occasions that the ORM hasn't allowed me to do what I want.

like image 132
Gary Chambers Avatar answered Oct 21 '22 20:10

Gary Chambers


creator of SQLAlchemy's response to the question is django considered now pythonic.. This shows a lots of difference and deep understanding of the system.

sqlalchemy_vs_django_db discussion in reddit

Note: Both the links are pretty long, will take time to read. I am not writing gist of them which may lead to misunderstanding.

like image 27
Kracekumar Avatar answered Oct 21 '22 19:10

Kracekumar