#33168 closed Cleanup/optimization (wontfix)

Meta.managed=False field not respected by makemigrations

Reported by: Javier Domingo Cansino Owned by: nobody
Component: Documentation Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have realised that Meta.managed field when set to False is not being respected by makemigrations, sample script to reproduce, relies on having django-admin in the path.

django-admin startproject sample
cd sample
python manage.py startapp unmanaged
echo 'from django.db import models

class ForeignUnmanagedTable(models.Model):
    rowid = models.AutoField(primary_key=True)

    class Meta:
        managed = False
        db_table = "foreign_unmanaged_table"
' > unmanaged/models.py

sed -i '/.*django.contrib.staticfiles.*/a     "unmanaged.apps.UnmanagedConfig", ' sample/settings.py
python manage.py makemigrations

Change History (6)

comment:1 Changed 14 months ago by Tim Graham

Resolution: invalid
Status: newclosed

Unmanaged models are tracked in migrations but database operations for unmanaged models aren't performed.

comment:2 Changed 14 months ago by Javier Domingo Cansino

I'm not sure if I understand your mesage, but bisecting the problem, it seems like a change in behaviour between python2 and python3.

Just in case, the problem is that given a model with managed=False, makemigrations generate migrations for that model. This behaviour is documented in [1] however it is only respected when running python 2 with older versions of Django.

[1] https://docs.djangoproject.com/en/3.2/ref/models/options/#managed

comment:3 Changed 14 months ago by Tim Graham

Migrations should be generated for unmanaged models (to track their state), however, running the migration should have no effect on the database.

comment:4 Changed 14 months ago by Javier Domingo Cansino

Resolution: invalid
Status: closednew

I have opened https://github.com/django/django/pull/14934 to clarify this in the documentation

comment:5 Changed 14 months ago by Javier Domingo Cansino

Component: MigrationsDocumentation
Type: BugUncategorized

comment:6 Changed 14 months ago by Mariusz Felisiak

Resolution: wontfix
Status: newclosed
Type: UncategorizedCleanup/optimization

I appreciate you'd like to reopen the ticket, however I don't think that an extra clarification is needed. It's already documented that:

If False, no database table creation, modification, 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.

We cannot document here all aspects of model handling.

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