I have a legacy database that I wish to use as a second database in my django project. I added the database to my settings file, and ran:
python manage.py inspectdb --database=images
The script finished in less than a second, and the results were astounding. It understood all my tables - or so I thought at first. When I tried to run:
python manage.py migrate --database=images
I got errors like this:
'unique_together' refers to the non-existent field 'commentaryId'.
All of the tables that raised errors were many-to-many linking tables containing two id fields that together formed the primary key (and thus had to be 'unique together').
Here is one of the models, created by inspectdb, that raised this error:
class Pagescanannotationscommentaries(models.Model):
pagescanannotationid = models.IntegerField(db_column='pageScanAnnotationId') # Field name made lowercase.
commentaryid = models.IntegerField(db_column='commentaryId') # Field name made lowercase.
class Meta:
managed = False
db_table = 'PageScanAnnotationsCommentaries'
unique_together = (('pageScanAnnotationId', 'commentaryId'),)
I found several questions like mine in stackoverflow, but the suggestions did not help me, or were obviously not relevant. But I did find a post in google groups that gave me the tip that fixed it: https://groups.google.com/forum/#!topic/django-users/_phTiifN3K0
But even then, my problem was a bit different, I found, and I explain in the answer below.
This was a bug in Django 1.8, see #25274. It has been fixed in 1.8.8. You should upgrade to the latest 1.8.x version.
Note that minor version upgrades, from 1.8.x to 1.8.x+1, only include bugfixes and security updates. You should always aim to use the latest minor version. Only major version upgrades, from 1.Y to 1.Y+1, may break compatibility as outlined in the deprecation timeline.