user2694306
user2694306

Reputation: 4060

Django migrate tables to new database

I originally had a django project with a single app and all models were defined in that app. The project, when initiated only used the default database. It has now become an unwieldy app that I'm trying to break down into smaller apps. Doing so, I want to use different databases for the different apps. I've setup new databases and a router in the settings.py file. However, I'm confused about how to migrate existing tables to the new databases.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'db_user_name',
        'PASSWORD': 'password',
        'HOST': 'hostname',
        'PORT': '3306',
    },
    'db2': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name2',
        'USER': 'db_user_name2',
        'PASSWORD': 'password2',
        'HOST': 'hostname2',
        'PORT': '3306',
    }    
}

I would want an app (e.g. app1) to be moved from default to db2. The router already knows to specify app1 to db2 but running migrations is doing nothing. Any ideas?

Upvotes: 6

Views: 7645

Answers (1)

user2694306
user2694306

Reputation: 4060

@knbk's answer was ultimately correct, except that the solution involved an additional step.

1. python manage.py migrate auth --database=db2
2. python manage.py migrate app1 --database=db2

Upvotes: 13

Related Questions