Reputation: 649
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.
Upvotes: 1
Views: 930
Reputation: 53729
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.
Upvotes: 2
Reputation: 649
django's inspectdb took my legacy field names as the field names for unique_together. I changed this so it used the properties in the model, and that solved the problem. I also added the _ before the id, as stated in the google groups post listed above. But I am not sure if that underscore was relevant. Right now, I don't want to mess with my set up, so I leave the test with the underscores (or rather without underscores) to someone else. :-) If I have time to test that, I will post what I find here.
Here is my working model code:
class Pagescanannotationscommentaries(models.Model):
pagescanannotation_id = models.IntegerField(db_column='pageScanAnnotationId') # Field name made lowercase.
commentary_id = models.IntegerField(db_column='commentaryId') # Field name made lowercase.
class Meta:
managed = False
db_table = 'PageScanAnnotationsCommentaries'
unique_together = (('pagescanannotation_id', 'commentary_id'),)
This change made the error message disappear.
Upvotes: 0