djongo icon indicating copy to clipboard operation
djongo copied to clipboard

ALTER_QUERY::FIXED:: Also rename __schema__ document for renamed collection

Open frikilax opened this issue 1 year ago • 1 comments

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

  1. 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')),
            ],
        ),
...
  1. 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" } } }
  1. Create a second migration to rename the model, and apply it:
...
        migrations.RenameModel(
            old_name="mymodel",
            new_name="mynewmodel",
        )
...
  1. 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

frikilax avatar Jan 03 '24 16:01 frikilax

Good catch

ELDiablO59152 avatar Jan 03 '24 16:01 ELDiablO59152