Wednesday 22 June 2016

Adding More tables to Sim application in Django

Yesterday I decided to have tables in separate databases and then have separate apps, each for one of these databases. But HS Rai sir pointed out that this was probably not the right way to go. And that made sense because such rigorous diversification in the initial stages of the project don't promise for a coherent future. Things could get more and more messier if I were to keep this approach up.
Now the aim was to have one database (by the name of Sim, of course) and have this database contain all the tables. Then came the issue of tables having the same names. To counter this I had two ideas:
  1. Change the names of the tables by augmenting their categories to their names. This way there will not be any redundancy in table names.
  2. Combine all tables with the same name into one and then add another field (say category) that can differentiate rows from different input tables.
The second approach sounds a bit more cleaner off the bat. But this would require the tables having the same name to have the exact same number and type of fields as well. This was not the case. So I had to drop this approach. It may be something I would have to come back in the future but for now I had to go with the first approach i.e. changing the names of the tables.
Once the table names were changed, things seemed easy. All I had to do was import the changed database into MySQL. Then import MySQL into my models.py file. And then a couple of django commands later, I should've been good to go. But no. There was something that I forgot. Something that I learnt (complained) about the hard way last time around. Yes, the necessity of having a primary key in every table.
First of all I decided to see if there already were any possible fields in all or some of these tables that can rightfully be set as the primary key. But I wasn't that lucky. So I decided to go all commando on the database and simply add a surrogate primary key to each table. I named this field as id on purpose (knowing Django would hate me if I named something else as the primary key). This field was integer and had auto increment feature for all the tables.
After doing this, I had to face no other problem. All the tables are there (with data) on the Django site.
Next order of business is to get this thing running on the experimental server.

No comments:

Post a Comment