Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django migrations not detecting all changes

I have the following models. BaseClass1 and BaseClass2 are abstract models used by models.

In this case, the model AdBreak is used by a viewset and a serializer. When I run python manage.py makemigrations, the changes on AdBreak model is detected. The model AdBreakStatus is not getting created.

Since, AdBreakStatus is linked to AdBreak, I am expecting a migration for AdBreakStatus also. Is my understanding wrong?

Edit

In the initial state, there was only AdBreak and BaseClass1 model. The new state, AdBreakStatus and BaseClass2 models were added. Some of the fields from AdBreak were moved to AdBreakStatus.

Thanks in advance for the help.

class BaseClass1(models.Model):
    class Meta:
        abstract=True
    timestamp = models.DateTimeField(auto_now_add=True)

class BaseClass2(models.Model):
    class Meta:
        abstract=True
    other_field = models.IntegerField()

class AdBreak(BaseClass1):
    class Meta:
        db_table = "ad_break"
    ad_break_id = models.AutoField(primary_key=True)
    ... # Other fields

class AdBreakStatus(BaseClass2):
    class Meta:
        db_table = "ad_break_status"
    ad_break = models.ForeignKey(AdBreak)
    ... # Other Fields
like image 422
Roopak A Nelliat Avatar asked Mar 29 '17 12:03

Roopak A Nelliat


People also ask

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

Why Makemigrations is not working?

This may happen due to the following reasons: You did not add the app in INSTALLED_APPS list in settings.py (You have to add either the app name or the dotted path to the subclass of AppConfig in apps.py in the app folder, depending on the version of django you are using). Refer documentation: INSTALLED_APPS.

What is the difference between Makemigrations and migrate in Django?

migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.

How do I reset Django migrations?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .


3 Answers

I solved it in multiple ways.

Solution 1

There is a serializer AdBreakSerializer which serializes the model AdBreak. Import AdBreakStatus model into the AdBreakSerializer file. Now AdBreakStatus model is detected and migrated.

Problem in this approach is that, the import is not used, and hence will not follow the standards.

Solution 2

Write the AdBreakStatus model class inside the same file of AdBreak. This will also solve the problem.


Finding / Understanding

The makemigrations script looks for models which are connected from urls.py. The script navigates from urls.py to all the viewset, then the corresponding serializers and models.

All the models which needs to be migrated should come in the path of this traversal. OR Only those models which are traversed in this manner will be migrated.

like image 96
Roopak A Nelliat Avatar answered Oct 18 '22 23:10

Roopak A Nelliat


In case someone does the same mistake as me, in my case it was because the field I added had the same name as an existing property. So make sure that the field name is not already used.

like image 30
Mei Avatar answered Oct 19 '22 00:10

Mei


Do this first:

python manage.py makemigrations 'your-app'
python manage.py migrate

If the above fails to detect changes, remove migration folder, open your database and open table django_migrations. You will see migrations listed associated to your app, delete the records and now execute makemigrations and migrate.

like image 2
nomad Avatar answered Oct 19 '22 00:10

nomad