Tuesday 21 June 2016

Django: Modeling into multiple databases

Models in Django are tables in a database. Each app in Django is allowed to have as many models as it needs in the model.py file. These models are then migrated to a database in your database product of choice. I had done only this so far.

But today I needed multiple databases to store multiple tables. I followed the following steps in attempt to complete this task:

1) Convert mdb files into mysql tables. This was done using the mdbtools kit in ubuntu.
2) Create new apps in the Django site.
3) Export the tables into the models.py file of the corresponding app.
4) Configure stuff in other files.
5) Run the server to see these work out.

Upon doing all of this, I was able to see the tables just fine, but there was no sign of any data in these tables. This was because I had simply imported the database schema from sql to my models. I never created a link for Django to also pick the data from the tables. And then I decided to follow the same method I used for SIM:

1) Clean up mysql from all the databases needed by Django.
2) Recreate these databases from the exported models.py files in Django.
3) Finally, populate the tables with data some how.

The approach was simple but since I had to deploy these models from more than one application into more than one databases, I ran into some problems:

1) Every migration went into the default database, so I had to learn to customize where the migrations should go.
2) Was not able to properly implement the above point.

So at the end of the day, I am stuck at the struggle of migrating my Django database into different databases. 

Once this is done, then I'll have to figure out how to populate a mysql table directly from an mdb file. 

No comments:

Post a Comment