Question

We have a php based site with MySQL database. We have lots of data there users, orders, invoices and etc. Now we wan to redesign our site application using Django and MySQL or even MongoDB.

It's really important for us to move all tables exactly as they used to be with no loss. Is there any way to do this? Can we define new models in Django and redirect them to existing data tables with same names and then import previous database?

If there's really a way to do it, what is it? I appreciate it if anyone knows a practical way.

Was it helpful?

Solution

Yikes! Migrations are hard.

First off, make a backup of your database that you can test with (this is probably a given, but it needs to be stated.) You can in fact use mysqldump to dump straight to a new MySQL database.

Most of what will get you started is in Django's documentation for working with "legacy databases".

Roughly speaking, set up a Django application with your database connection parameters and the name of the database. You'll need to edit the DATABASES setting and assign values to these keys:

NAME
ENGINE
USER
PASSWORD
HOST
PORT

To get started, there is a built in utility called inspectdb that can generate models based on your database.

python manage.py inspectdb

Keep in mind that this is only a first pass, and you'll likely have to fix some of it by hand. Once you get the output cleaned up, save it as models.py and add it to your INSTALLED_APPS setting. You'll also need to set managed=True in each of your model's internal class Meta.

After all this is done, run

python manage.py migrate

You will probably have to tweak this over and over to get it right. I highly recommend having backups and a good (maybe automated) setup to keep testing and tweaking to get it right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top