Running a database migration with RunPython on a second database fails
python3 manage.py migrate --database=app
The problem is that the apps.get_model method takes the default database which has already the newest migrations.
Does not work:
def copy_cpr_cents_to_euros(apps, schema_editor):
User = apps.get_model('accounting', 'User')
User.objects.filter(...);
Works:
def copy_cpr_cents_to_euros(apps, schema_editor):
User = apps.get_model('accounting', 'User')
User.objects.using('app').filter(...);
Is there a way to use the given database in the migration, so in this case "app" without making expliclitly declaring it, since it should work for both databases?
So something like:
User.objects.using(database_name).filter(...)
schema_editor.connection.alias
contains the string of the current database with which the migration was started.
So each RunPython-migration must use this alias to manually select the right db.
Example:
def copy_cpr_cents_to_euros(apps, schema_editor):
User = apps.get_model('accounting', 'User')
db = schema_editor.connection.alias
User.objects.using('app').using(db).filter(...)