Django makemigations not updating database

When I make changes to models.py, I am expecting django to update the database structure for me when I run python3 manage.py makemigrations or python3 manage.py makemigrations appname. It doesn’t detect any changes. I have had this issue once before and had to delete everything in the database to update it, which seems a bit drastic. What am I doing wrong?

This is the new line I have just added:

l3_interfaces = JSONField 

(py38-venv) [xxxxxx@xxxxxxn]$ python3 manage.py makemigrations
No changes detected

Contents of models.py

from django.db import models                                                                                    #Generic for models
from django.contrib.auth.models import User                                                                     #Required for dynamic user information
from django.forms import ModelForm                                                                              #Custom form
from jsonfield import JSONField

#Unique order. Top of heirachy tree
class Order(models.Model):

    order_name = models.CharField(max_length=100, unique=True)#, null=True, blank=True)                         #Unique name of order
    created_by = models.ForeignKey(User, related_name='Project_created_by', on_delete=models.DO_NOTHING)        #Person who created the order
    created_at = models.DateTimeField(auto_now_add=True)                                                        #Date/Time order was created

    def __str__(self):
        return self.order_name

#For CE router definition. Required for all orders.
class Ce_Base(models.Model):

    #Hardware models of router
    ROUTER_MODELS = (
            ('CISCO2901', 'CISCO2901'),
            ('ISR4331', 'ISR4331'),
            ('CISCO1921', 'CISCO1921'),
            ('ISR4351', 'ISR4351'),
            ('ISR4451', 'ISR4451'),
            ('ISR4231', 'ISR4231'),
            ('ISR4431', 'ISR4431'),
            ('ISR4461', 'ISR4461'),
            )
    #Available regions in which the router can reside.
    REGION = (
            ('1', '1'),
            ('2', '2'),
            ('3', '3'),
            ('4', '4'),
            ('5', '5'),
            ('6', '6'),
            ('7', '7'),
            )

    #Properties of CE Base
    ce_hostname = models.CharField(max_length=15)                                               #Hostname of router i.e. XXXXXXXXX
    new = models.BooleanField()                                                                 #Whether the router currently exists on the network
    location = models.TextField(null=True, blank=True)                                          #Address of the POP site/rack location. Required if new.
    router_model = models.CharField(max_length=200, null=True, choices=ROUTER_MODELS)           #Hardware model of the router i.e ISR4351. Required if new.
    region = models.CharField(max_length=200, null=True, choices=REGION)                        #Region in which the router resides. Required if new.

    #Foreign key to tie CE Base to an order
    order_reference = models.ForeignKey(Order, null=True, on_delete=models.CASCADE)             #Order reference

    l3_interfaces = JSONField                                                                   #Layer 3 interfaces

>Solution :

It’s because you didn’t add a new field on your model. What you should have written is the following:

l3_interfaces = JSONField()

Also, a quick note on semantics:

  • makemigrations does not update your DB structure, it generates the migration files
  • migrate does actually update your DB structure by applying the generated files

Leave a Reply