Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 746
user2694306 Avatar asked Nov 16 '16 17:11

user2694306


People also ask

What is Django migration table?

Django uses a database table called django_migrations . Django automatically creates this table in your database the first time you apply any migrations. For each migration that's applied or faked, a new row is inserted into the table.

What is the difference between Makemigrations and migrate in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.


1 Answers

@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
like image 142
user2694306 Avatar answered Sep 27 '22 00:09

user2694306