SQLAlchemy: An ORM Solution

Hayden Nault
3 min readMar 29, 2023

--

SQL Alchemy is one of the most popular ORMs today for working with relational databases in python. It has become so popular and widely used for three reasons, it is simple to implement and import into your files, it enables the user to develop code more efficiently and quickly, and finally it does not require the user to have knowledge of SQL to get started.

Why use an ORM?

“There are two ways you can connect a python application to a relational database: a low-level approach and a high-level one.” The low-level approach requires installing the relational database management system on your own machine, and then you actually have to write SQL commands for everything you hope to accomplish (too much busywork). The high-level approach (the better approach) is to use something like SQLAlchemy, known as an object relational mapper or ORM.

An ORM like SQLAlchemy is the middleman between you and the database engine. You can define normal Python object, classes, and methods, and the ORM will translate them in to the SQL instructions for you.

As I stated before, the main benefit of SQLAlchemy is that it allows you to write in python to connect your database schema to whatever you have created using Python. You do not have to write SQL statements to create or query your database, which leads to more efficient and quicker programming solutions. It does the busy work for you, and because it does this, it means you have to write less code to achieve the same goal.

Introducing Flask:

Another reason to use an ORM like SQLAlchemy is because you can also use Web Application Frameworks like Flask to connect to the internet and develop your web applications. The best part about Flask is that it is written in python, so it works fairly seamlessly with an ORM like SQLAlchemy.

Flask is known for being easy to use, as in, it does not have a huge learning curve. It is also supposed to be very explicit in order to improve readability. I am going to learn more about Flask in the next phase of my course here at Flatiron School, I’ll provide an update after that!

But wait, there’s more:

Finally, I want to touch on the fact that SQLAlchemy is not the only ORM out there. While it is very popular, there are other solutions like Django that are also widespread.

The primary difference between the two ORMs is that while SQLAlchemy uses “data mapper implementation”, Django ORM uses “active record implementation”. In plain English, this means that Django ORM has a direct relationship between the code and the database, making it rigid but maybe adds simpler functionality. Meanwhile, SQLAlchemy has a separation between the database and the objects, which makes CRUD actions slightly more cumbersome but enables more flexibility.

I won’t get too into the details here, but I did want to point out that there are many viable options out there for your ORM solutions, and there is no one that is best. Choose the best ORM for your needs, without too much extra “stuff” going on to complicate things.

Connect with me: https://www.linkedin.com/in/hayden-nault/

Resources:

Pro-tip: Django Orm vs SQLAlchemy in 2021. EBS Integrator. (2021, April 15). Retrieved March 28, 2023, from https://ebs-integrator.com/blog/django-orm-vs-sql-alchemy/#:~:text=Differences%20%E2%80%93%20SQL%20Alchemy%20vs%20Django,with%20the%20overall%20model%20object.

What is Flask Python. What is Flask Python — Python Tutorial. (n.d.). Retrieved March 28, 2023, from https://pythonbasics.org/what-is-flask-python/

Sqlalchemy. Full Stack Python. (n.d.). Retrieved March 28, 2023, from https://www.fullstackpython.com/sqlalchemy.html

Uwase, A. (2021, February 8). Here is the reason why SQLAlchemy is so popular. Medium. Retrieved March 28, 2023, from https://towardsdatascience.com/here-is-the-reason-why-sqlalchemy-is-so-popular-43b489d3fb00

--

--