Why does the deletion of an unmanaged models create migration?

Hey, I have the following model:

class User(HasDates):
    class Meta:
        managed = False

    username = models.CharField(max_length=255, null=True, blank=True)

when I created the model, the following migration was created:

    operations = [
        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('username', models.CharField(blank=True, max_length=255, null=True)),
            ],
            options={
                'managed': False,
            },
        ),
    ]

now, while I understand why this migration wont affect the db, when I deleted the model from my project the following migration was created:

    operations = [
        migrations.DeleteModel(
            name='User',
        ),
    ]

my question is: how the delete migration knows to not really delete the User model from the DB?

would appreciate your answers :slight_smile:

it’s just saving metadata of what you’re doing, creating/deleting. I’m not sure why you care, it’s like the way “git commit” works with version control on Github

Well I want to sure that this migration wont drop the table and Im nut sure where this metadata saved

If you remove a model and make a migration, then it will drop the table and restructure the database. You shouldn’t really be looking at the migration files. You can query information from your database in the django shell (python manage.py shell)

A quick test shows that migrate does not delete the underlying tables when defined with managed = False.

The easiest way for you to validate this would be to make a copy of the database (you only need to copy the schema, no need to copy all the data), and try it!