djongo
djongo copied to clipboard
ALTER_QUERY::FIXED:: Also rename __schema__ document for renamed collection
Description
When doing a migration including RenameModel()
operations, collections are correctly reamed, but not their corresponding schema document, resulting in out-of-sync database information for fields and auto-incremental primary IDs
How to reproduce
- Create a first migration creating a Model, with at least a primary id, and apply it:
...
migrations.CreateModel(
name='mymodel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
...
- Check in MongoDB that the model has a corresponding schema document:
db.getCollection('__schema__').find({name: {$regex: "mymodel"}})
{ "_id" : ObjectId("65952a1101314dfd724e8d92"), "name" : "test_mymodel", "auto" : { "field_names" : [ "id" ], "seq" : 0 }, "fields" : { "id" : { "type_code" : "int" } } }
- Create a second migration to rename the model, and apply it:
...
migrations.RenameModel(
old_name="mymodel",
new_name="mynewmodel",
)
...
- Check MongoDB's schema collection again, and see that the document "name" value wasn't updated:
db.getCollection('__schema__').find({name: {$regex: "mymodel"}})
{ "_id" : ObjectId("65952a1101314dfd724e8d92"), "name" : "test_mymodel", "auto" : { "field_names" : [ "id" ], "seq" : 0 }, "fields" : { "id" : { "type_code" : "int" } } }
Implications
After launching the 2 migrations, schema document is not synced with the latest Model name, so new Model instances are created without the incremental ID value
Good catch