9 thoughts on “When should someone use Raw SQL instead of Django’s ORM”

  1. This is one of those “when you need to use it, you’ll know” situations. The ORM is extremely powerful so it might be that you never need the raw functionality at all.

  2. Most often when I have to integrate with a database that I don’t control or I’m migrating functionality from an existing app that I can copy the SQL from, generally speaking both of these being for reports more often than not.

    Can I redo it in ORM? Sure, but deadlines being what they are good enough code gets you paid.

  3. There are some DBMS features that Django’s ORM does not support. Whenever you need that feature, raw SQL is your only option.

    Some examples from MySQL:
    – group_concat
    – Fulltext search

    Examples will differ depending on the DBMS.

  4. If you are using the Postgres database backend, but the database is not 100% wire-compatible with Postgres (something like TimescaleDB, ClickHouse, Yugabyte, etc.) and you need to use some of the functionality that Postgres doesn’t understand


Leave a Comment