Automatic db router for migrations in django

Raff89

I am building a Django powered site and I want to have separate databases for some of the apps, I build a flexible router that routes each app to predefined database, this works fine. The problem is that when I am migrating my models I have to set the --database parameter every time and I find this annoying and redundant. Also many times I flooded my default database with tables from migrated app (by forgetting to add --database). I experimented with allow_migrate(...) function in my router but all I could achieve is safety mechanism that will not run the migration if I forget to specify the database. My question is: is there a way to set up a automatic database selection for models migrations in Django? My approach might be wrong, it surprises me that no one seems to have done that before.

Kevin Christopher Henry

I don't know of any way to do that automatically. However, a simple approach might be to write your own version of the migrate command that calls the Django migrate command multiple times with the appropriate --database and app arguments.

According to the author of Django migrations, this is a conscious design decision, so I wouldn't expect it to change: "Just like syncdb, migrate only runs on one database at a time, so you must execute it individually for each database, as you suggest. This is by design."

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mark specific Django migrations as fake migrations during test DB setup

Django on Kubernetes Deployment: Best practices for DB Migrations

django.db.migrations.exceptions.InconsistentMigrationHistory

django.db.migrations.exceptions.NodeNotFoundError

django.db.migrations.exceptions.CircularDependencyError

ValueError while making db migrations in django

EntityFramework Core automatic migrations

Why does my Django app attempt to create migrations, when it is defined to not allow migrations in a router?

Why django_migrations table is created even though router does not allow migrations?

How to force migrations to a DB if some tables already exist in Django?

Deleting Migration Files and Getting django.db.migrations.exceptions.NodeNotFoundError:

Django make migrations issue changing to new Postgres DB

New Django app initial DB migrations of an existing database

Do django db_index migrations run concurrently?

django.db.migrations.RenameModel and AutoField sequence name

from django.db import models, migrations ImportError: cannot import name migrations

Django migrations

Entity Framework, Automatic apply Migrations

Running django migrations on windows for remote mssql db with native windows db drivers

Django DB Router Sending Table to default DB instead of Correct One

Apostrophe CMS db migrations

CodeFirst DB Migrations with Azure

Getting error while syncdb django.db.utils.ProgrammingError: permission denied for relation django_migrations

django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"

Steps to Troubleshoot "django.db.utils.ProgrammingError: permission denied for relation django_migrations"

Django Migrations ValueError: Could not find manager in django.db.models.manager

How do you solve django.db.migrations.exceptions.NodeNotFoundError in Django?

How to set default column value in the db using django 1.7.3/postgres migrations?

How to read data from an External Server and apply migrations on a local Db file in Django?