An ORM works by:
- Mapping database tables to classes – Tables become objects, columns become attributes.
- Translating code into SQL queries – ORM automatically generates SQL commands like SELECT, INSERT, or UPDATE.
- Managing relationships – ORM simplifies working with foreign keys and joins. For example, in Django ORM:
user = User.objects.get(id=1) # Translates to SELECT * FROM users WHERE id=1;
user.name = “New Name”
user.save() # Generates an UPDATE query
This eliminates the need to write SQL manually while keeping the power of relational databases.