Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#29987 closed Bug (fixed)

Deleting non-managed model does not generate a DeleteModel in migrations

Reported by: Daniel Bowring Owned by: Tim Fiedler
Component: Migrations Version: 2.1
Severity: Normal Keywords: autodetector managed model delete
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If you have a model like:

class NonManagedModel(models.Model):
    some_field = models.TextField()

    class Meta:
        managed = False

and run makemigrations, a migration including a CreateModel will be generated

$ python3 manage.py makemigrations nmapp
Migrations for 'nmapp':
  nmapp/migrations/0001_initial.py
    - Create model NonManagedModel

However, if you remove the model from models.py, no migration with a DeleteModel is created

$ python3 manage.py makemigrations nmapp
No changes detected in app 'nmapp'

It seems like the intention was to include this for unmanaged models, but is being skipped (see ​django.db.migrations.autodetector)

Change History (10)

comment:1 by Simon Charette, 8 years ago

Keywords: autodetector managed model delete added
Triage Stage: Unreviewed → Accepted

I haven't reproduced but looking at the auto-detector code that seems like a legitimate report. Would you be interested in providing a patch to get it fixed?

comment:2 by Tim Fiedler, 8 years ago

Owner: changed from nobody to Tim Fiedler
Status: new → assigned

comment:3 by Tim Fiedler, 8 years ago

Type: Bug → Uncategorized

I had provide a ​PR which for sure needs some tests. But to be honest now i'm thinking of the idea of a unmanaged model. Or to be precise... Is this a realy a bug ? Regarding the Documentation this seems to be a desired behaviour:)

​https://docs.djangoproject.com/en/2.1/ref/models/options/

If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed=False. All other aspects of model handling are exactly the same as normal. This includes


Last edited 8 years ago by Tim Graham (previous) (diff)

comment:4 by Tim Fiedler, 8 years ago

Owner: Tim Fiedler removed
Status: assigned → new

comment:5 by Tim Graham, 8 years ago

Type: Uncategorized → Bug

comment:6 by Simon Charette, 8 years ago

Has patch: set
Patch needs improvement: set

Thanks for the quick patch Tim!

Left a few comments for improvement on the PR. Please uncheck Patch needs improvement once you've addressed them.

Thanks!

comment:7 by Tim Fiedler, 8 years ago

Owner: set to Tim Fiedler
Patch needs improvement: unset
Status: new → assigned

comment:8 by Tim Graham <timograham@…>, 8 years ago

Resolution: → fixed
Status: assigned → closed

In b07273a:

Fixed #29987 -- Detected unmanaged model deletions.

comment:9 by Andi Albrecht, 7 years ago

Shouldn't this be mentioned in the release notes for 2.2? It took me a while to figure out why I have new migrations after upgrading to 2.2.

comment:10 by Mariusz Felisiak, 7 years ago

Bugfixes are not mentioned in release notes for major releases.

Note: See TracTickets for help on using tickets.
Back to Top