djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Renaming many-to-many key field leads to run time errors

Open hyough opened this issue 3 years ago • 0 comments

Going through an exercise of renaming model fields to have a lower case initial letter. In particular did this with some many-t0-many keys, e.g.

class CollectionSite(models.Model): . . itemsCollected = models.ManyToManyField(RecyclableItem, verbose_name="Can process")

Original name was "ItemsCollected".

Run makemigrations, and the script correctly identifies the renaming of the field. HOWEVER when I subsequently run the site, any attempt to insert into the CollectionSite table (using Djano admin or other methods) results in a DatabaseError, with the root cause reported as DuplicateKey. Examining the database reveals a number of problems.

  1. An entry has been created in the collection collectionsite_itemsCollected with the two referenced IDs ("collectionsite_id" and "item_id"), a standard mongodb "_id" field, but no django "id" field.
  2. In the schema collection, there is no entry with the name field "collectionsite_itemsCollected". The original version "collectionsite_ItemsCollected" is still there.

Manually correcting the name in the schema collection, and deleting the document with the missing django "id" restores normal function.

I have repeated this process a number of times with different keys on different collections and the behaviour appears consistent.

The migration should rename the schema entry automatically.

hyough avatar May 25 '21 08:05 hyough