Skip to content

ORM

Object-Relational Mapping (ORM) is a layer between code and database. It allows working with data as objects in the language rather than writing SQL manually.

Example

# without ORM
cursor.execute("SELECT * FROM products WHERE id = 1")

# with ORM
product = Product.objects.get(id=1)

Pros and Cons

  • Pros: easier, safer (protection against injections), portable between databases.
  • Cons: hides SQL details, may be slower on complex queries.

When Not Needed

For one-off scripts and direct access to foreign schema, it is often simpler and more transparent to write SQL directly.

See Also

← All Terms