Hi everyone,
straight to the point.
I’ve worked with Django and PostgreSQL before, using a single database for all project apps, as is commonly done.
Currently, I’m facing a different scenario: I need each app to have its own isolated database, using MariaDB, with its own user and password. This is because we’re migrating from a legacy PHP system that is already structured this way, and part of the goal is to preserve existing backup and operational processes. So using a single schema/database for all Django apps is not a viable option for us.
I’ve reviewed the documentation and understand this isn’t a common use case in Django, although it is technically possible using database routers. However, the main problem I’ve encountered relates to the django_migrations
table.
Current operational context
To handle this scenario, we’ve made the following architectural decisions:
- All migration tracking and control is centralized in the main database (
CORE_P_DJANGO
, thedefault
alias), which also holds Django’s internal tables (auth
,admin
, etc.). - Business models for each app are migrated into their respective databases (
CORE_P_UGO
,CORE_P_KERNEL
, etc.). - We allow Django to automatically create a
django_migrations
table in each app database, but we ignore its contents for migration tracking purposes. - We use a custom command,
manage.py migrate_db
, which applies migrations to the appropriate database but registers them only inCORE_P_DJANGO
.
Important notes
- We never rely on the
django_migrations
table present in the app-specific databases. - We never manually modify those tables.
- All migration tracking is centralized in the
django_migrations
table ofCORE_P_DJANGO
.
I’m wondering if this is a reasonable approach. I’ve also considered running a separate Django project for each app using Apache + mod_wsgi, but I’m concerned about the performance and maintainability impact. Right now we have a few apps, but once the migration is complete there will be 40+ apps.
So, here are my questions for you:
- Does this approach make sense?
- Is there a better practice for handling multiple isolated databases per app in Django?
- Is there anything in Django’s roadmap regarding better support for this kind of setup?
- Am I overcomplicating things, and maybe missing a simpler solution?
Thanks in advance for any insights or shared experiences!