ORM for Python

Sabanova Rubabe
Pragmatech
Published in
4 min readOct 22, 2020

Before giving information about some ORMs for Python, it would be better to talk about an ORM concept in general.

So what is an ORM(Object Relational Mapping) ?

The logic behind Object Relational Mapping is being able to write queries and manipulating data using an object-oriented paradigm. It gives an opportunity to deal with a database by using our language (which in this article we will mention Python) instead of SQL. It means that ORMs allows us to create, read, update and delete data and schemas in the database with Python code.

What are the advantages of using ORMs?

  • It might save a lot of time for whom don’t write in SQL much. So because you use the language you already were using can speed up the process.
  • ORM makes switching between various relational databases(e.g. from MySQL to PostgreSQL) much easier because most functions remain same.
  • Some of the queries will perform better than if you wrote them yourself.

What are the disadvantages of using ORMs?

  • Because ORM is a different concept, in order to use an ORM, first you should learn it, which learning something new and adjust can take time.
  • For whom that is good at SQL, using ORM can be less efficient and useful. Because it won’t give you as much opportunities as you would get when you write queries yourself.
  • In general, because of the fact that ORM abstracts what is happenning under the hood, it would lead you be less capable to erase the problems.
  • The extra conflicts can appear when setting configuration and so on.

As we already are more aware of an ORM concept, we can move on to some examples of ORM for Python.

The Python ORMs that are mostly used these days are :(I have also added their original documentations for whom that is interested)

The Django ORM

SQLAlchemy

Peewee

PonyORM

SQLObject

Django ORM

The Django web framework has its own built-in object-relational mapping module that can be used to deal with database written in SQLite, PostgreSQL, MySQL and so on. It is also possible not to use this default ORM for Django web framework in particular but others as SQLAlchemy because though Django ORM helps somehow, there are complains much that using Django ORM is mostly harder than just writing the codes in raw SQL.

SQLAlchemy

SQLAlchemy is mostly considered as the best one among other ORMs beacuse of its simplicity, speed and also some other features that other ORMs don’t have. One of the advantages it has is SQLAlchemy allows writing Python code to map data from the database to the applications’ Python objects. No SQL is required to create, maintain and query the database. It provides us working with Python objects instead of writing bridge code to get data in and out of relational tables.

SQLAlchemy can be used with Flask, Pyramid( which they are Python frameworks), Django(though Django doesn’t support it officially yet, there are some ways to switch to SQLAlchemy).

Peewee

Peewee is simpler and easier than most ORMs as Django ORM or SQLAlchemy which makes it useful for the ones who is just getting started web development. Peewee can be used with nearly any web framework which is good news to hear. But at the end of the day because the analogy used by the core Peewee author is that Peewee is to SQLAlchemy as SQLite is to PostgreSQL which makes it hard to use it in big projects.

Pony ORM

Pony also looks like other Python ORMs that is available but it also has differences as Pony makes it much easier to write even so complex SQL codes in pythonic way, also in terms of speed, Pony is one of the best among them. In general we can say that Pony supporting nearly everything that other ORMs have makes it powerful but maybe the biggest problem Pony has is it doesn’t support migration yet (but Pony developers have confirmed they are working on this).

--

--