Reputation: 4979
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.
Upvotes: 27
Views: 23026
Reputation: 86
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.
Upvotes: 3
Reputation: 11
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.
Upvotes: 0
Reputation: 668
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.
Upvotes: 1
Reputation: 2428
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.
Upvotes: 0
Reputation: 79
Ensure your app is registered in the settings.py, if so then try running:
python manage.py makemigrations "app_name"
Upvotes: 5
Reputation: 747
This happens when you forget to register your app in setting.py file.
Upvotes: 2
Reputation: 2012
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:
Upvotes: 20
Reputation: 4979
The models.py needs to be inside the application package, rather than in the main directory for the makemigrations to work.
Upvotes: 2
Reputation: 519
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
.
Upvotes: 3
Reputation: 4364
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.
Upvotes: 44