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/, django/, django/, and django/ — 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 instantiates a backend’s wrapper subclass (such as DatabaseWrapper), BaseDatabaseWrapper attaches five collaborator objects (plus SchemaEditorClass, instantiated on demand by BaseDatabaseWrapper):
| Component | Base class | Responsibility |
|---|---|---|
| Wrapper | Base class Base |
Responsibility Owns the DB-API handle, cursor creation, transaction state, savepoints, "operators" dict, and "data_types" mapping. |
| Features | Base class Base |
Responsibility Declares ~150 boolean capabilities and version gates (can_rollback_ddl, has_, supports_). |
| Operations | Base class Base |
Responsibility Generates vendor SQL fragments (quote_name, limit_offset_sql, for_update_sql, year_) and row converters (get_). |
| Introspection | Base class Base |
Responsibility Reads live catalog metadata (table_names, get_, get_constraints) for "inspectdb", tests, and schema editing. |
| Client & Validation | Base class Base / Base |
Responsibility Launches interactive vendor shells ("dbshell") and runs backend-specific system checks on model fields. |
Each base class lives in django/: 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’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:
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:
can_rollback_ddl: PostgreSQL and SQLite execute"CREATE TABLE"and"ALTER TABLE"inside a transaction, so a crashing migration rolls back cleanly leaving zero half-applied schema changes. MySQL and Oracle execute an implicit"COMMIT"before every DDL statement (can_rollback_ddl = False), which is whyBaseDatabaseSchemaEditorrefuses to run DDL inside an active.execute atomic()block on MySQL/Oracle and why a mid-migration crash on MySQL leaves the table half-altered!has_select_for_update: SQLite locks the entire database file on write and ignores row-level"SELECT ... FOR UPDATE"(has_select_for_update = False). Aselect_for_update()call that silently succeeds outside a transaction in SQLite tests will immediately raiseTransactionManagementErroron PostgreSQL or MySQL if you forgot to wrap the view intransaction!.atomic can_return_rows_from_bulk_insert: Notice"dynamic"under MySQL:DatabaseFeaturesis a.can_return_rows_from_bulk_insert @cached_propertyon the MySQL backend because MariaDB supports"INSERT ... RETURNING"while upstream MySQL does not — soQuerySetpopulates primary keys on PostgreSQL, SQLite, and MariaDB, but leaves them.bulk_create Noneon MySQL and Oracle.supports_timezones: Only PostgreSQL stores native timezone-aware"timestamp with time zone"values (supports_timezones = True). SQLite, MySQL, and Oracle strip timezone offsets before storage and rely onBaseDatabaseOperationsto reattach.get_db_converters datetimewhen rows come back..timezone .utc supports_logical_xor: Only MySQL supports native boolean"XOR"; on SQLite, PostgreSQL, and Oracle,WhereNode.as_sqlrewritesQ(a) ^ Q(b)into(a OR b) AND 1 = CASE WHEN ...(as we recorded in Q objects and the WhereNode tree).interprets_empty_strings_as_nulls: Oracle treats""and"NULL"identically (True), forcingQueryto treat every non-nullable.is_nullable CharFieldas nullable on Oracle!supports_on_delete_db_default: New for Django6.1’s database-levelDB_SET_DEFAULT(DatabaseOnDelete), PostgreSQL and SQLite support"ON DELETE SET DEFAULT"cleanly while MySQL ("InnoDB") and Oracle setFalse.
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.