---
django: 6.1.3 (f16e25dcb9fe55734ba4ea2b00ac34405b7df74a)
---

# 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` (`django/db/backends/base/base.py:BaseDatabaseWrapper`).

## The five-class anatomy of a Django database backend

When `ConnectionHandler.create_connection` instantiates a backend's wrapper subclass (such as `django/db/backends/sqlite3/base.py:DatabaseWrapper`), `BaseDatabaseWrapper.__init__` attaches five collaborator objects (plus `SchemaEditorClass`, instantiated on demand by `BaseDatabaseWrapper.schema_editor`):

| Component | Base class | Responsibility |
| --- | --- | --- |
| Wrapper | `BaseDatabaseWrapper` | Owns the DB-API handle, cursor creation, transaction state, savepoints, `"operators"` dict, and `"data_types"` mapping. |
| Features | **`BaseDatabaseFeatures`** | Declares ~150 boolean capabilities and version gates (`can_rollback_ddl`, `has_select_for_update`, `supports_timezones`). |
| Operations | **`BaseDatabaseOperations`** | 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 | `BaseDatabaseIntrospection` | Reads live catalog metadata (`table_names`, `get_table_description`, `get_constraints`) for `"inspectdb"`, tests, and schema editing. |
| Client & Validation | `BaseDatabaseClient` / `BaseDatabaseValidation` | Launches interactive vendor shells (`"dbshell"`) and runs backend-specific system checks on model fields. |

Each base class lives in `django/db/backends/base/`: `BaseDatabaseFeatures` (`django/db/backends/base/features.py:BaseDatabaseFeatures`), `BaseDatabaseOperations` (`django/db/backends/base/operations.py:BaseDatabaseOperations`), `BaseDatabaseIntrospection` (`django/db/backends/base/introspection.py:BaseDatabaseIntrospection`), `BaseDatabaseClient` (`django/db/backends/base/client.py:BaseDatabaseClient`), and `BaseDatabaseValidation` (`django/db/backends/base/validation.py: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](../sql/compiler.md).

## 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 (`django/db/backends/sqlite3/features.py:DatabaseFeatures`, `django/db/backends/postgresql/features.py:DatabaseFeatures`, `django/db/backends/mysql/features.py:DatabaseFeatures`, and `django/db/backends/oracle/features.py:DatabaseFeatures`):

```text recording=ch09-database
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 why `BaseDatabaseSchemaEditor.execute` refuses to run DDL inside an active `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`). A `select_for_update()` call that silently succeeds outside a transaction in SQLite tests will immediately raise `TransactionManagementError` on PostgreSQL or MySQL if you forgot to wrap the view in `transaction.atomic`!
- **`can_return_rows_from_bulk_insert`:** Notice `"dynamic"` under MySQL: `DatabaseFeatures.can_return_rows_from_bulk_insert` is a `@cached_property` on the MySQL backend because **MariaDB** supports `"INSERT ... RETURNING"` while upstream **MySQL** does not — so `QuerySet.bulk_create` populates primary keys on PostgreSQL, SQLite, and MariaDB, but leaves them `None` on 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 on `BaseDatabaseOperations.get_db_converters` to reattach `datetime.timezone.utc` when rows come back.
- **`supports_logical_xor`:** Only MySQL supports native boolean `"XOR"`; on SQLite, PostgreSQL, and Oracle, `WhereNode.as_sql` rewrites `Q(a) ^ Q(b)` into `(a OR b) AND 1 = CASE WHEN ...` (as we recorded in [Q objects and the WhereNode tree](../sql/q-and-where.md)).
- **`interprets_empty_strings_as_nulls`:** Oracle treats `""` and `"NULL"` identically (`True`), forcing `Query.is_nullable` (`django/db/models/sql/query.py:Query.is_nullable`) to treat every non-nullable `CharField` (`django/db/models/fields/__init__.py:CharField`) as nullable on Oracle!
- **`supports_on_delete_db_default`:** New for Django `6.1`'s database-level `DB_SET_DEFAULT` (`DatabaseOnDelete`), PostgreSQL and SQLite support `"ON DELETE SET DEFAULT"` cleanly while MySQL (`"InnoDB"`) and Oracle set `False`.

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](schema-editor.md)), 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](transactions.md).
