Skip to content

Conversation

frikilax
Copy link

@frikilax frikilax commented Jan 3, 2024

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

@ELDiablO59152
Copy link

Good catch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.