django makemigrations not detecting new model

Question:

I used makemigrations earlier in order to make my Django app aware of the tables in my legacy MySql database, and it worked fine. It generated models.py. Now, I want to add a new “UserDetails” model to my app:

class UserDetails(models.Model):
    user = models.CharField(max_length=255)
    telephone = models.CharField(max_length=100)

After saving the file, I ran the following command in the command prompt:

python manage.py makemigrations

But makemigrations is not seeing the new class I wrote inside the models.py, as it always outputs ‘No changes detected’

Am I doing it correctly or is there anything wrong with it? I checked my MySQL db and confirmed that no table named UserDetails already exists.

Asked By: logeeks

||

Answers:

You have to run python manage.py makemigrations first, Second, you have to run python manage.py migrate to sync db.

If you mkdir a folder to save your model, you have to add it to __init__.py.

Answered By: huang

I’ve met this problem during development and this combination helps me:

python manage.py makemigrations mymodule

This command create migrations for the specific module. It should be in INSTALLED_APPS, but you’ll get warning if it’s not in there.

python manage.py migrate

Also, mention answer by xiaohen, I’ve create packages with PyCharm, so I’ve got init.py file by default.

Answered By: Dracontis

The models.py needs to be inside the application package, rather than in the main directory for the makemigrations to work.

Answered By: logeeks

I encountered similar issue (“No changes detected” when adding new models) when using Django 1.11, and solved by importing the new models (actually better to import all models) in the __init__.py in models package:

from .student import Student
from .teacher import Teacher

It’s written here:

Answered By: Alan Zhiliang Feng

This happens when you forget to register your app in setting.py file.

Answered By: Shah Vipul

Ensure your app is registered in the settings.py,
if so then try running:

 python manage.py makemigrations "app_name"
Answered By: wizer_102

Also, note that when the abstract attribute is set to True in the models Meta class, the model will be ignored by makemigrations.

Check that the atrribute is set to False or remove it altogether.

Answered By: PowerAktar

Another issue may be if you have two ForeignKeys that point to the same model without a related_name to distinguish between them. E.g.

fk1 = models.ForeignKey(OtherModel)
fk2 = models.ForeignKey(OtherModel)

Django should give you a warning: HINT: Add or change a related_name argument to the definition for 'app.Model.fk1' or 'app.Model.fk2'.

But I didn’t see it until I restarted django.

Answered By: Chris

I want to also add apart from the answers provided above if problem still not solved, please do ensure your migrations dir is available in your app folder if not available ensure to create one and make sure to create an

__init__.py

file inside so that python interpreter will be able to identify that the dir is python dir. Please edit if I might have misused some terminologies. Hope this helps.

Answered By: Gabriel J

I would like to add: If the model Meta class has an app_label, and it is different to the INSTALLED_APPS label – it will fail.

Answered By: Derek Smith
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.