Django 6.1.3

9.2 Connections, transactions, and migrations

How database backends diverge beneath one API

Where do SQLite, PostgreSQL, MySQL, and Oracle actually differ inside Django’s backend architecture — features, operations, validation, and vendor quirks?

Django ships four first-party database engines — django/db/backends/sqlite3/, django/db/backends/postgresql/, django/db/backends/mysql/, and django/db/backends/oracle/ — behind one shared QuerySet and migration API. Rather than scattering if vendor == "postgresql": branches across the ORM, Django isolates every vendor difference behind a clean database backend composition inside BaseDatabaseWrapper.

The five-class anatomy of a Django database backend

When ConnectionHandler.create_connection instantiates a backend’s wrapper subclass (such as DatabaseWrapper), BaseDatabaseWrapper.__init__ attaches five collaborator objects (plus SchemaEditorClass, instantiated on demand by BaseDatabaseWrapper.schema_editor):

Component Base class Responsibility
Wrapper Base class BaseDatabaseWrapper Responsibility Owns the DB-API handle, cursor creation, transaction state, savepoints, "operators" dict, and "data_types" mapping.
Features Base class BaseDatabaseFeatures Responsibility Declares ~150 boolean capabilities and version gates (can_rollback_ddl, has_select_for_update, supports_timezones).
Operations Base class BaseDatabaseOperations Responsibility Generates vendor SQL fragments (quote_name, limit_offset_sql, for_update_sql, year_lookup_bounds_for_datetime_field) and row converters (get_db_converters).
Introspection Base class BaseDatabaseIntrospection Responsibility Reads live catalog metadata (table_names, get_table_description, get_constraints) for "inspectdb", tests, and schema editing.
Client & Validation Base class BaseDatabaseClient / BaseDatabaseValidation Responsibility Launches interactive vendor shells ("dbshell") and runs backend-specific system checks on model fields.

Each base class lives in django/db/backends/base/: BaseDatabaseFeatures, BaseDatabaseOperations, BaseDatabaseIntrospection, BaseDatabaseClient, and BaseDatabaseValidation. The ORM, SQLCompiler, and migration engine query these capability flags and helper methods instead of branching on backend names — with only one escape hatch, SQLCompiler.compile’s "as_<vendor>" hook covered in SQLCompiler.as_sql and parameter binding.

Concrete divergences across the four built-in backends

Look directly at seven high-impact capability flags pulled from Django 6.1.3’s four pinned DatabaseFeatures classes:

Recorded on Django 6.1.3 ch09-database script
2. Cross-backend DatabaseFeatures divergence in Django 6.1.3
  feature                                sqlite3  postgres     mysql    oracle
  can_rollback_ddl                          True      True     False     False
  has_select_for_update                    False      True      True      True
  can_return_rows_from_bulk_insert          True      True   dynamic     False
  supports_timezones                       False      True     False     False
  supports_logical_xor                     False     False      True     False
  interprets_empty_strings_as_nulls        False     False     False      True
  supports_on_delete_db_default             True      True     False     False

Every row in that matrix corresponds to a concrete runtime trap when developers test locally on SQLite and deploy to PostgreSQL or MySQL:

Add two more structural quirks that bite teams migrating between backends: SQLite lacks native "ALTER COLUMN" and therefore triggers full table rebuilds via _remake_table (which we measure in BaseDatabaseSchemaEditor and table rewrites), and Django 6.1.3’s minimum database versions sit at SQLite 3.37, PostgreSQL 15, MySQL 8.4 (MariaDB 10.11), and Oracle 19.

With connections and backend differences clear, we turn to the state machine that protects multi-statement writes across every one of those backends: autocommit, Atomic, savepoints, and "needs_rollback" in Autocommit, atomic, and savepoints.