You are hereBlogs / David Grant's blog / Moving Data from One Database to Another with Django

Moving Data from One Database to Another with Django

By David Grant - Posted on 03 March 2012

So I have a django database I'm working on and I decided I wanted to do the development in sqlite3 instead of mysql. I decided to do this because it makes it easier, for example, to have someone else work on HTML/CSS if I can just give them a directory, tell them to run a bash script and go to http://localhost:8000, rather than them having to do all that AND setup a mysql server. Sure, that can also be done with a script, but with sqlite things are just a hell of a lot easier in some ways.

I decided I wanted to take the data out of my mysql database and load it into a sqlite db. Not so simple, you can't just take an SQL dump from mysql and import it into sqlite3. I started Googling around for "mysql to sqlite" and didn't really get anywhere. I then realized that Django can already talk to sqlite and mysql transparently without me having to know any SQL. So I thought about writing my own python modules to do this, but it turns out someone has already done it.

The django-extensions project has a command called "dumpscript" that "Generates a Python script that will repopulate the database using objects. The advantage of this approach is that it is easy to understand, and more flexible than directly populating the database, or using XML." This was exactly what I wanted.

I love django-extensions. Very helpful library. But wouldn't the stock ' dumpdata' have done the trick?

Dumpdata/loaddata is not really that easy with full imports.

Many times, models depend on the proper order of importing so a big import fails. 'loaddata' uses the ORM, so it will trigger your models, which are now operating in a very unusual half-empty setup. loaddata seems to create models when the foreignkeys don't exist yet, breaking the import of the actual data later on. The data also includes the contenttypes table, so it should be cleaned first.

I wish it would be so easy, but sadly in practice I didn't always experience that. In the end I typically dumped the data of a few apps separately, and imported them one by one. :-/

You might find django-smuggler useful as well.

Do you want to know more of the django database? Then you surely have to read this and you will get more idea regarding moving one database to another. This was very informative for me as it tells us it is more flexible and can easily be understood.
microsoft outlook problem solving

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <s> <img> <h2> <h3>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.