djangomakemigrations

Unable to makemigrations in Django


Problem statement:

My basic model:

from django.db import models


class Xyz(models.Model):
    name = models.CharField(max_length=100)

I tried through pycharm & normal terminal. I am using UBuntu 20.04LTS.

I deleted all migration files then reran makemigrations for app1 , it only picked old changes. did not pick new Xyz model.

So perhaps there is something wrong in the way I am creating this new model2.py file. But I am creating it normally like model1.py. I even tried to copy paste and rename the same file. All my folder have init.py in them.


Solution

  • Try importing the app1.models.model2.Xyz class to the app1/models/__init__.py as it was resolved on my side.

    app/models/model2.py

    from django.db import models
    
    
    class Xyz(models.Model):
        name = models.CharField(max_length=100)
    

    Run 1: Unsuccessful

    app/models/__init__.py

    from models.model1 import *
    

    Make migrations

    $ python3 app/manage.py makemigrations
    (0.000) SELECT @@SQL_AUTO_IS_NULL; args=None
    (0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
    (0.000) SELECT VERSION(); args=None
    (0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
    (0.000) SHOW FULL TABLES; args=None
    No changes detected
    

    Run 2: Successful

    app/models/__init__.py

    from models.model1 import *
    from models.model2 import Xyz  # Add this line
    

    Make migrations

    $ python3 app/manage.py makemigrations
    (0.000) SELECT @@SQL_AUTO_IS_NULL; args=None
    (0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
    (0.000) SELECT VERSION(); args=None
    (0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
    (0.000) SHOW FULL TABLES; args=None
    Migrations for 'app':
      app/migrations/0002_xyz.py
        - Create model Xyz