All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is ORM and why is it needed?

Post cover: What is ORM and why is it needed?
This content has been automatically translated from Ukrainian.
When we work with databases, we usually have to write SQL queries - samples, inserts, data updates, etc. But when the project grows, these requests become hundreds, and it becomes difficult to maintain them.
This is where it comes into play ORM (Object-Relational Mapping) - technology that allows you to work with a database through programming language objects.
ORM “translates the data from the tables in the database as objects that can be handled in code.
For example, instead of writing:
SELECT * FROM users WHERE id = 1;
at Ruby on Rails we just do:
user = User.find(1)
and we get a User object that has methods, properties and behavior like the regular Ruby class.
Benefits of ORM
  • SQL minimum -most requests are generated automatically.
  • Fewer mistakes - ORM checks types, relationships, validations.
  • Faster development - CRUD operations (create, read, update, delete) are written in several lines.
  • Portability - it's easy to change the DBMS (for example, from PostgreSQL to MySQL) without rewriting requests.
  • Conveniently working with connections - has_many, belongs_to, has_one, etc.
Disadvantages of ORM
  • Productivity - sometimes ORM generates “heavy” requests that need to be optimized manually.
  • Black box - it's hard to understand what's going on under the hood.
  • Not for everything - complex SQL queries or analytical samples are better written manually.
Examples of ORM
  • Ruby - ActiveRecord
  • Python - SQLAlchemy, Django ORM
  • JavaScript - Sequelize, Prisma
  • PHP - Eloquent (Laravel)
In short. ORM is a bridge between the world of objects and tables. It allows developers to think less about SQL and more about business logic. But like any tool, ORM is worth understanding so as not to turn convenience into a performance trap.

This post doesn't have any additions from the author yet.

What is NP-complexity?
16 Sep 19:31

What is NP-complexity?

meme code
meme code@memecode
Offset vs Cursor Pagination in Rails: What to Choose and Why
24 Sep 15:22

Offset vs Cursor Pagination in Rails: What to Choose and Why

meme code
meme code@memecode
What is Row Security in PostgreSQL and why is it Rails developers
04 Oct 19:06

What is Row Security in PostgreSQL and why is it Rails developers

meme code
meme code@memecode
What is ivar in Ruby /Rails?
19 Oct 20:12

What is ivar in Ruby /Rails?

meme code
meme code@memecode
Basic methods of authentication in the API
19 Oct 20:26

Basic methods of authentication in the API

meme code
meme code@memecode
How do OAuth 1 differ from OAuth 2
19 Oct 20:34

How do OAuth 1 differ from OAuth 2

meme code
meme code@memecode
MCP: A new internet where sites communicate with AI
04 Nov 11:43

MCP: A new internet where sites communicate with AI

meme code
meme code@memecode
Why is TOON better than JSON when working with AI?
14 Nov 15:14

Why is TOON better than JSON when working with AI?

meme code
meme code@memecode