Read the full blog here.
Written by Conrad Rowlands, Team Leader and Developer, DSCallards
Visit www.dscallards.com for more information on our Development Services.
This is the blogspot for Developer Solutions - DSCallards Development division. We have been developing solutions for the modern world and working together with our customers, partners and suppliers to invest in the ever advancing technologies of the software industry. You can call us on 0800 652 4050 or email us at sales@dscallards.com.
$ python manage.py schemamigration MyApplication --initial
A cursory examination of my app and I now see a migrations folder, contained within is a 0001_initial.py file which contains the code required to upgrade my database. Great, without examining too closely I continued to apply the patches to the database
$ python manage.py migrate MyApplication
This too went of without issue and a cursory test of one of the models proved that my changes had indeed been applied:->>> from MyApplication.models import MetricIndex
>>> MetricIndex.objects.all() []
Doesn’t look like much, but the empty block of course means that I have returned an empty array and NOT a big fat juicy error which is good! However my cursory examination of the upgrade script meant that I missed the detail…. I am using abstract inheritance for my tables because with many of the tables they have a lot of common fields. These fields are defined within an abstract model interface and then each table inherits from that. The upshot is I should have 1 database table created for each concrete class and no tables created for the abstract classes. This was not what the scripts had generated within them. My model classes were defined thus:-
class ModelBase(): Name =models.CharField(max_length=255) Order =models.IntegerField(default=-1) class meta: abstract =True class MetricIndex(ModelBase): pass
Strange it should have worked… I should have had one MetricIndex table with two fields and no ModelBase table. The answer turned out to be frustratingly simple ( and remember, this worked correctly with dbSync). My abstract class was not defined correctly, ‘class meta’ should in fact have been ‘class Meta’. I applied the changes to the code as below
class ModelBase(): Name =models.CharField(max_length=255) Order =models.IntegerField(default=-1) class Meta: abstract =True class MetricIndex(ModelBase): pass
I then prepared the database script to migrate to the next iteration of the schema thus…
$ python manage.py schemamigration MyApplication --auto
And applied the schema changes as below
$ python manage.py migrate MyApplication
Voila…. I now had one properly formed MetricIndex table and no ModelBase table. Onward and upward and I urge you to use this great tool!