Django Multiple Databases Tutorial: Aliases, Routers, and Boundaries
Multiple database settings give Django named connections, but the application must decide which records belong to each. Explicit aliases and routers direct operations. Separate connections also introduce transaction and relationship boundaries that cannot be ignored simply because the code uses familiar model methods.
Here's a popular Django Multiple Databases video — watch it, then summarize it with one click. Or paste any other YouTube link below.
— or paste your own YouTube link —
Want to keep the key points from a YouTube video? Paste the link below to turn it into structured notes.
Why Use Django Multiple Databases Tutorial: Aliases, Routers, and Boundaries
- Declare distinct database connection aliases
- Select an alias for a queryset
- Route operations through documented rules
- Select the target connection before schema changes
- Keep transaction scope visible across connections
Frequently Asked Questions
Does declaring another connection move existing records automatically?
A connection alias only makes another database available. Data placement, copying, and migration choices remain separate operations that need an explicit plan rather than happening when a settings entry is added.
Can one atomic block roll back every configured database?
Transaction management applies to the selected database connection. Coordinating writes across connections requires additional design; an ordinary atomic block does not automatically create a distributed transaction covering every database.
Are foreign keys between separate databases ordinary supported relationships?
Django does not provide general cross-database referential integrity through normal foreign keys. Keep related data together where required or design an explicit application boundary instead of assuming the database can enforce that relationship.
When is using an alias clearer than a router?
Explicit alias selection is useful when a particular operation clearly targets a known connection. Routers centralize broader placement rules, but their behavior should remain understandable when reading and debugging application operations.
Why run migrations with the intended database selected?
Each database has its own schema state and migration history. Applying changes to the wrong connection can leave the actual target missing required tables while changing a different database unexpectedly.