Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django orm versus sqlachemy, are they basically the same thing?

When using django, I believe you can swap out the built-in orm for sqlalchemy (not sure how though?).

Are they both basically the same thing or there is a clear winner between the 2?

like image 525
Blankman Avatar asked Sep 13 '10 13:09

Blankman


People also ask

Is Django ORM based on SQLAlchemy?

SQLAlchemy ORM is similar to Django ORM, but at the same time, they differ. SQLAlchemy ORM uses a different concept, Data Mapper, compared to Django's Active Record approach.

Is SQLAlchemy better than Django ORM?

In addition to this, since it interacts directly with the database, you can simply run the queries against the database without actually using the ORM. Plus, SQLAlchemy is much more powerful than Django, albeit with a little higher learning curve.

Can we use SQLAlchemy in Django?

In some cases, Django and SQLAlchemy can be used together. The main use case I got to see numerous times in the real world is when Django is used for all regular CRUD operations, while SQLAlchemy is used for the more complex queries, usually read-only queries.

Does Django have its own 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.


1 Answers

When using django, I believe you can swap out the built-in orm for sqlalchemy (not sure how though?).

You can use SQLAlchemy in your Django applications. That doesn't mean you can "swap" out the ORM though. Some of Django's built-in batteries would cease to work if you completely replace Django's ORM with SQLAlchemy. For instance the Admin app wouldn't work.

I have read about people using both. Django's ORM to get the batteries to work and SQLAlchemy to tackle complex SQL relationships and queries.

Are they both basically the same thing or there is a clear winner between the 2?

They are not the same thing. SQLAlchemy is more versatile than Django's ORM. For instance while Django's ORM tightly couples the business logic layer and the persistence layer into models, SQLAlchemy will let you keep them separate.

On the other hand SQLAlchemy has a steeper learning curve and would be an overkill for many projects. The existing migration tools for SQLAlchemy may not easily integrate with Django.

All said, I wouldn't presume to declare either a "winner". They are broadly similar tools but with their own strengths, weaknesses and best-fit situations.

While you are on the subject it wouldn't hurt to read this (dated but relevant) blog post about the short comings of the Django ORM and how it compares with SQLAlchemy.

like image 132
Manoj Govindan Avatar answered Oct 04 '22 17:10

Manoj Govindan