JiangLing
JiangLing

Reputation: 367

how to switch to a new database

I want to deploy my django project to the production environments, and associated it with an new empty database, and I did as follows :

  1. Create an new empty database
  2. Updated settings.py and pointed the database name to the new one
  3. Deleted the migrations folder under my App
  4. Run python manage.py runserver and no errors returned
  5. Run python manage.py makemigrations and python manage.py migrate

but only auth related tables created ( like auth_user , auth_group ... ), no databases tables created for my Apps

How should I do for this situation to move to the new database for my project?

Upvotes: 5

Views: 17385

Answers (5)

Max
Max

Reputation: 31

I also made a migration and used Umer's answer, but encountered this error when running
python manage.py loaddata data.json --database=new:
json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

Fixed it by using the --output option for dumpdata instead of redirect and excluding tables auth.permission and contenttypes:

python3 manage.py dumpdata --exclude auth.permission --exclude contenttypes --output data.json

Information about excluding tables is from here

dumpdata reference

Upvotes: 0

Umer
Umer

Reputation: 580

Firstly, you should not have deleted the migrations. Now, make all the migrations again which you have deleted.

python manage.py makemigrations app_name

Do this for all the apps of which you have deleted the migrations. Now, add your new database to settings.py. Do not remove the old one yet. For example, if I were adding a MySQL database, I would have added the following to the DATABASES dictionary in settings.py:

'new': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'databasename',
    'USER': 'databaseusername',
    'PASSWORD': 'databasepassword',
    'HOST': 'localhost',
    'PORT': '3306',
}

I have named the database as 'new'. Now we have two databases 'default' and 'new'. Now you have to create tables in the new database by running the migrations on the new database:

python manage.py migrate --database=new

You can follow these additional steps if you want to transfer your data to the new database. First, clear the new database:

python manage.py flush --database=new

Now export data from the old database into a json file:

python manage.py dumpdata>data.json

Import this data into the new database:

python manage.py loaddata data.json --database=new

Now you can remove the 'default' database and rename the 'new' database to 'default'.

The procedure mentioned in this answer is taken from my blog.

Upvotes: 15

JulienD
JulienD

Reputation: 7293

If your database has a new name, i.e. not "default", you need to specify it to migrate:

python manage.py migrate --database <newdb>

Upvotes: 0

Piyush S. Wanare
Piyush S. Wanare

Reputation: 4933

Just check the output of python manage.py makemigrations command, if it is showing no change detected then you need to check that have you added that app in your INSTALLED_APPS = [] in settings.py file or it might be the problem because you have deleted migration folder.Because if is there any database connectivity error it will show you that while doing makemigrations.

Upvotes: 0

Sayse
Sayse

Reputation: 43300

  1. Deleted the migrations folder under my App

This was your mistake, you deleted the migrations - including the initial migrations. So when you go to makemigrations you haven't got the initial migration available.

So you need to run makemigrations <app_name> to at least get the initial migration.

If you were to do this again, don't delete the migrations, just change the database settings and then migrate.

Upvotes: 12

Related Questions