[PYTHON] Django: Migration doesn't reflect model in DB

Not reflected

As the title suggests, even if I migrated from Django, it was not reflected in the DB and I suffered, so I will record it.

environment

・ Windows10 Enterprise -Docker Desktop for Windows: 2.1.0.1 ・ MySQL: 5.7.28 (on Docker) ・ Django: 3.0 (on Docker)

Background

When I added a new model (referred to as ** model A **) to models.py and migrated it, it was not reflected in the DB. Since changes in other models are reflected, it is estimated that only ** model A ** is a problem.

I tried various things

I tried various things with reference to the following articles.

・ What to do if you're migrating with Django but it's not reflected in your DB [Django output] How to reset Migrations with Django (keeping the existing database) ・ I can't migrate with django ・ Reflection of Django's models.py (python manage.py makemigrations / migrate) Phenomenon that column is added to model but not added to migration file ・ What to do when python manage.py migration doesn't work [Django]

Generally, it was said that if you delete the migration history with "python manage.py migrate --fake hoge zero", delete the files in the folder, then make migration and migrate, it will be reflected, but it is wiped out.

Was it you

So, once I drop the DB and try to migrate again ... ** Model A ** and another model (** Model B ) are not reflected. When I read "models.py" with a good feeling of "?", Only these two models have the characters " managed = False **" in "class Meta:".

models.py


    class Meta:
        managed = False
        db_table = 'temp'

I deleted it and migrated it again, and it was done. Easy. I just didn't understand the basics. ** Model B ** is pasting the format copied from somewhere, and I copied and pasted this to create ** Model A **, so it seems that I got caught in this trap. By writing "managed = False", it seems that migration will not be managed. That shouldn't be reflected. (I wrote in "History" that the changes of other models were reflected, but I did not try it with ** model B **)

That's why

It is a mystery why the copy source included this, but since it worked for the time being ... Under lighthouse living. I decided to look it up a little closer.

Recommended Posts

Django: Migration doesn't reflect model in DB
Model changes in Django
How to reflect CSS in Django
Implement a Custom User Model in Django
Django model: ManyToManyField
Models in Django
How to reflect ImageField in Django + Docker (pillow)
Forms in Django
How to get multiple model objects randomly in Django
Django DB initialization commands
High Performance Django --Model
[Django] Manage settings like writing in settings.py with a model
Create an easy-to-use follow model in Django using ManyToManyField through