270

I was trying to create migrations within an existing app using the makemigrations command but it outputs "No changes detected".

Usually I create new apps using the startapp command but did not use it for this app when I created it.

After debugging, I found that it is not creating a migration because the migrations package/folder is missing from an app.

Would it be better if it creates the folder if it is not there or am I missing something?

Dave Mackey
  • 4,306
  • 21
  • 78
  • 136
Dilraj
  • 2,791
  • 2
  • 15
  • 9

38 Answers38

521

To create initial migrations for an app, run makemigrations and specify the app name. The migrations folder will be created.

./manage.py makemigrations <myapp>

Your app must be included in INSTALLED_APPS first (inside settings.py).

hestellezg
  • 3,309
  • 3
  • 33
  • 37
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 32
    Any idea why they force us to specify the app ? – maazza Dec 21 '16 at 14:02
  • 70
    @maazza you need to specify the app name if the app does not have `migrations` folder. This could happen if you created the app manually, or you have upgraded from an older version of Django that didn't have migrations. – Alasdair Dec 21 '16 at 14:21
  • 24
    @maazza Actually you need a python package (with `__init__.py`) named 'migrations' in the app. – Jibin Feb 21 '18 at 11:22
  • 6
    Sounds like something that Django should handle automatically. – duality_ Jun 04 '18 at 12:15
  • 4
    @duality_ this [is by design](https://docs.djangoproject.com/en/2.0/topics/migrations/#adding-migrations-to-apps) - Django doesn't assume that you want migrations for your app. If it created migrations for all apps, it could lead to errors when you run `migrate`. – Alasdair Jun 04 '18 at 12:24
  • 2
    this issue cause me two days to debug, and your answer simply solve my problem, i just wrongly assume that makemigrations will automatically applly to all installed app. – Luk Aron Nov 11 '19 at 04:13
  • I faced similar issue; I had deleted the migrations folder, so if it happens to you check that you have a `migrations` folder with a file `__init__.py` in it. – babis21 Aug 27 '22 at 09:55
  • This answer is the correct one, you might create a new sub app under the django app that you have created and you forget to add it to `INSTALLED_APPS` in `settings.py`. Perhaps I have a project called `marketing` in my `settings.py` it would be something like. ``` INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "polymorphic", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "contacts", ] ``` – abmap Mar 12 '23 at 18:27
99

My problem (and so solution) was yet different from those described above.

I wasn't using models.py file, but created a models directory and created the my_model.py file there, where I put my model. Django couldn't find my model so it wrote that there are no migrations to apply.

My solution was: in the my_app/models/__init__.py file I added this line: from .my_model import MyModel

  • This happended to be the solution for me as well, but I do not understand why this is. Does anyone have some insight as to what may be causing this? – Paul in 't Hout Apr 12 '19 at 12:06
  • 2
    Django has a default paths of where to look for your models. If the project structure is different, and the models are not in the usual place, they need to be imported there. – Karina Klinkevičiūtė Apr 15 '19 at 21:33
  • 1
    @KarinaKlinkevičiūtė what if I need to remove such models? – Danil Aug 05 '19 at 20:03
  • @DaniilMashkin I imagine you would need to remove imports also. This is one of the ways of structuring your project (not the only one) and you have to deal with additional tasks that comes with it if you choose it :) – Karina Klinkevičiūtė Aug 06 '19 at 08:54
  • @KarinaKlinkevičiūtė I removed all imports and models. When run `makemigration` I had no changes – Danil Aug 07 '19 at 08:57
  • That's weird. I haven't deleted the models. Maybe try making migrations only for one app, like so: `python manage.py makemigrations some_app` – Karina Klinkevičiūtė Aug 08 '19 at 13:19
  • 3
    I used the "classic" architecture for models, then I migrated to the "models folder" architecture, and any migration is still detected on my existing models. However, now, when creating a *new* model, I have this issue. Your solution works well, but it let my codebase kind of inconsistent because sometimes there is an import, sometimes not. Maybe there is a better solution. I guess Django should propose a settings with a list of folders to look for when trying to find new models. – David Dahan Feb 23 '20 at 21:13
89

There are multiple possible reasons for django not detecting what to migrate during the makemigrations command.

  1. migration folder You need a migrations package in your app.
  2. INSTALLED_APPS You need your app to be specified in the INSTALLED_APPS .dict
  3. Verbosity start by running makemigrations -v 3 for verbosity. This might shed some light on the problem.
  4. Full path In INSTALLED_APPS it is recommended to specify the full module app config path 'apply.apps.MyAppConfig'
  5. --settings you might want to make sure the correct settings file is set: manage.py makemigrations --settings mysite.settings
  6. specify app name explicitly put the app name in manage.py makemigrations myapp - that narrows down the migrations for the app alone and helps you isolate the problem.
  7. model meta check you have the right app_label in your model meta

  8. Debug django debug django core script. makemigrations command is pretty much straight forward. Here's how to do it in pycharm. change your script definition accordingly (ex: makemigrations --traceback myapp)

Multiple databases:

  • Db Router when working with django db router, the router class (your custom router class) needs to implement the allow_syncdb method.

makemigrations always creates migrations for model changes, but if allow_migrate() returns False,

user1134422
  • 1,161
  • 8
  • 7
  • 3
    Covered many scenarios regarding the problem, should be the accepted answer. – Krishh Jun 15 '18 at 00:57
  • Another possibility: The wrong name is being imported, i.e. importing a field from forms instead of fields, or importing a model from forms instead of models. An example: `from recurrence.forms import RecurrenceField` but it should have been `from recurrence.fields import RecurrenceField`. – hlongmore Apr 27 '19 at 04:37
  • 1
    One more reason. Make sure that the model is used within a route for the website (via admin or otherwise). "The `makemigrations` script looks for models which are connected from `urls.py`". Found here https://stackoverflow.com/questions/43093651/django-migrations-not-detecting-all-changes – Kyle Apr 18 '20 at 14:05
  • cmd example: `python manage.py makemigrations -v 3 ` – Charlie 木匠 Jul 02 '20 at 16:19
  • When I add a table, and then add a Foreign Key reference this new table at same time. It has to be divided to 2 steps: pre step: add INSTALLED_APPS to settings. 1) create new table: python manage.py makemigrations ; 2) add Foreign Key: python manage.py makemigrations – Charlie 木匠 Jul 02 '20 at 16:31
  • @user1134422 this is one of the best answers on all of StackOverflow to any question. It answers lots of people's problems tracing to the same appearance. Thank you. – Scott Stafford May 13 '21 at 19:57
  • I went through all points but it still wasn't working for me. Turns out, the superclass models.Model was missing from the data model class in models.py . Check that too! – Pink Flying Elephant Mar 24 '22 at 20:56
  • If the above list doesn't help try mentally following your code through the documented start up process and see if that gives you any clues. It's not quite as bad as you think; better than spening all day with no migrations: https://docs.djangoproject.com/en/4.0/ref/applications/#how-applications-are-loaded This is why magic is bad. Magic is supposed to hide the implementation details that you don't need to know, but with Django every time you do something interesting you need to dig inside the magic. It's not Pythonic. One day, there will be no Django. Django is not forever. – NeilG Jun 01 '22 at 08:24
  • Make sure your model is imported somewhere else in your project... – MarMat Jul 05 '23 at 06:03
36

I've read many answers to this question often stating to simply run makemigrations in some other ways. But to me, the problem was in the Meta subclass of models.

I have an app config that says label = <app name> (in the apps.py file, beside models.py, views.py etc). If by any chance your meta class doesn't have the same label as the app label (for instance because you are splitting one too big app into multiple ones), no changes are detected (and no helpful error message whatsoever). So in my model class I have now:

class ModelClassName(models.Model):

    class Meta:
        app_label = '<app name>' # <-- this label was wrong before.

    field_name = models.FloatField()
    ...

Running Django 1.10 here.

onekiloparsec
  • 2,013
  • 21
  • 32
27

Another thing that will cause this is a trailing comma after the field which will cause the field to skipped during makemigrations:

class MyModel(models.Model):
    name = models.CharField(max_length=64, null=True)  # works
    language_code = models.CharField(max_length=2, default='en')  # works
    is_dumb = models.BooleanField(default=False),  # doesn't work

I had a trailing , in one line perhaps from copy&paste. The line with is_dumb doesn't create a model migration with ./manage.py makemigrations because Python thinks it is a tuple, and Django doesn't consider it a field.

Ben Davis
  • 13,112
  • 10
  • 50
  • 65
yvess
  • 1,992
  • 19
  • 17
  • 2
    The trailing comma can cause bugs elsewhere as well; the comma makes the statement a tuple, so `is_dumb` is equal to `(models.BooleanField(default=False), )` which `makemigrations` doesn't know how to convert into a database column. – hlongmore Apr 27 '19 at 04:42
  • this happened to me, should be a warning if it doesn't know what to do – Daniel Benedykt May 24 '21 at 21:08
  • Yup, this solution worked for me. – mufazmi Oct 30 '22 at 13:47
20

It is a comment but should probably be an answer.

Make sure that your app name is in settings.py INSTALLED_APPS otherwise no matter what you do it will not run the migrations.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'blog',
]

Then run:

./manage.py makemigrations blog
tread
  • 10,133
  • 17
  • 95
  • 170
19

update: One should make sure that an __init__.py file exists in the migrations folder before trying:

./manage.py makemigrations <myapp1> <myapp2> ... <myappN>


There are sometimes when ./manage.py makemigrations is superior to ./manage.py makemigrations <myapp> because it can handle certain conflicts between apps.

Those occasions occur silently and it takes several hours of swearing to understand the real meaning of the dreaded No changes detected message.

Therefore, it is a far better choice to make use of the following command:

./manage.py makemigrations <myapp1> <myapp2> ... <myappN>

raratiru
  • 8,748
  • 4
  • 73
  • 113
  • 1
    4 hours of debugging and THIS was the problem! Had __init__.py added to .gitignore. File was there locally, so everything worked. When I pushed to Heroku, that app's migrations were ignored! This fixed it. – Stormbytes May 19 '23 at 22:51
13

Method : 1

Step : 1

Make sure your app must be included in INSTALLED_APPS in settings.py

Stpe : 2

python manage.py makemigrations <appname>

if same message shows (No changes detected)

!Warning This is Very Risky For Your Project So Make Sure You Have Backup For Your Project Before Applying The Method 2.

Method 2

rename your app name and make new app using :

django-admin startapp <appname>

copy all .py files except from the old app

  • migration folder
  • pycache folder
  • init.py
  • test.py file if you didn't wrote code in it


and paste into the new app which you made recently

remember you have to make exact the same name for new app otherwise you have to make more changes in the project.

11
  1. Make sure your app is mentioned in installed_apps in settings.py
  2. Make sure you model class extends models.Model
Amandeep Singh
  • 503
  • 3
  • 5
11

My problem was much simpler than the above answers and probably a far more common reason as long as your project is already set up and working. In one of my applications that had been working for a long time, migrations seemed wonky, so in a hurry, I did the following:

rm -r */migrations/*
rm db.sqlite3
python3 manage.py makemigrations
No changes detected

Whaat??

I had mistakenly also removed all the __init__.py files :( - Everything was working again after I went in and:

touch ads1/migrations/__init__.py

For each of my applications then the makemigrations worked again.

It turns out that I had manually created a new application by copying another and forgot to put the __init__.py in the migrations folder and that confinved me that everything was wonky - leading my making it worse with an rm -r as described above.

Hope this helps someone from swearing at the "No changes detected" error for a few hours.

drchuck
  • 4,415
  • 3
  • 27
  • 30
10

I had copied a table in from outside of django and the Meta class defaulted to "managed = false". For example:

class Rssemailsubscription(models.Model):
    id = models.CharField(primary_key=True, max_length=36)
    ...
    area = models.FloatField('Area (Sq. KM)', null=True)

    class Meta:
        managed = False
        db_table = 'RSSEmailSubscription'

By changing managed to True, makemigrations started picking up changes.

Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
Dan Cogswell
  • 475
  • 3
  • 16
7

Another possible reason is if you had some models defined in another file (not in a package) and haven't referenced that anywhere else.

For me, simply adding from .graph_model import * to admin.py (where graph_model.py was the new file) fixed the problem.

Nick Lothian
  • 1,427
  • 1
  • 15
  • 31
5

When adding new models to the django api application and running the python manage.py makemigrations the tool did not detect any new models.

The strange thing was that the old models did got picked by makemigrations, but this was because they were referenced in the urlpatterns chain and the tool somehow detected them. So keep an eye on that behavior.

The problem was because the directory structure corresponding to the models package had subpackages and all the __init__.py files were empty. They must explicitly import all the required classes in each subfolder and in the models __init__.py for Django to pick them up with the makemigrations tool.

models
  ├── __init__.py          <--- empty
  ├── patient
  │   ├── __init__.py      <--- empty
  │   ├── breed.py
  │   └── ...
  ├── timeline
  │   ├── __init__.py      <-- empty
  │   ├── event.py
  │   └── ...
atoledo
  • 435
  • 5
  • 12
5

This might hopefully help someone else, as I ended up spending hours trying to chase this down.

If you have a function within your model by the same name, this will remove the value. Pretty obvious in hindsight, but nonetheless.

So, if you have something like this:

class Foobar(models.Model):
    [...]
    something = models.BooleanField(default=False)

    [...]
    def something(self):
        return [some logic]

In that case, the function will override the setting above, making it "invisible" to makemigrations.

vpetersson
  • 1,630
  • 2
  • 17
  • 18
3
INSTALLED_APPS = [

    'blog.apps.BlogConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

]

make sure 'blog.apps.BlogConfig', (this is included in your settings.py in order to make your app migrations)

then run python3 manage.py makemigrations blog or your app name

Marlon Abeykoon
  • 11,927
  • 4
  • 54
  • 75
Piyush Chandra
  • 159
  • 1
  • 4
3

A very dumb issue you can have as well is to define two class Meta in your model. In that case, any change to the first one won't be applied when running makemigrations.

class Product(models.Model):
    somefield = models.CharField(max_length=255)
    someotherfield = models.CharField(max_length=255)

    class Meta:
        indexes = [models.Index(fields=["somefield"], name="somefield_idx")]

    def somefunc(self):
        pass

    # Many lines...

    class Meta:
        indexes = [models.Index(fields=["someotherfield"], name="someotherfield_idx")]
nbeuchat
  • 6,575
  • 5
  • 36
  • 50
3

In my case i forgot to insert the class arguments

Wrong:

class AccountInformation():

Correct

class AccountInformation(models.Model):
JSRB
  • 2,492
  • 1
  • 17
  • 48
3

I had a different issue while creating a new app called deals. I wanted to separate the models inside that app so I had 2 model files named deals.py and dealers.py. When running python manage.py makemigrations I got: No changes detected.

I went ahead and inside the __init__.py which lives on the same directory where my model files lived (deals and dealer) I did

from .deals import *
from .dealers import *

And then the makemigrations command worked.

Turns out that if you are not importing the models anywhere OR your models file name isn't models.py then the models wont be detected.

Another issue that happened to me is the way I wrote the app in settings.py:

I had:

apps.deals

It should've been including the root project folder:

cars.apps.deals
elad silver
  • 9,222
  • 4
  • 43
  • 67
2

I solved that problem by doing this:

  1. Erase the "db.sqlite3" file. The issue here is that your current data base will be erased, so you will have to remake it again.
  2. Inside the migrations folder of your edited app, erase the last updated file. Remember that the first created file is: "0001_initial.py". For example: I made a new class and register it by the "makemigrations" and "migrate" procedure, now a new file called "0002_auto_etc.py" was created; erase it.
  3. Go to the "pycache" folder (inside the migrations folder) and erase the file "0002_auto_etc.pyc".
  4. Finally, go to the console and use "python manage.py makemigrations" and "python manage.py migrate".
2

I know this is an old question but I fought with this same issue all day and my solution was a simple one.

I had my directory structure something along the lines of...

apps/
   app/
      __init__.py
      app_sub1/
           __init__.py
           models.py
      app_sub2/
           __init__.py
           models.py
      app_sub3/
           __init__.py
           models.py
   app2/
      __init__.py
      app2_sub1/
           __init__.py
           models.py
      app2_sub2/
           __init__.py
           models.py
      app2_sub3/
           __init__.py
           models.py
    main_app/
      __init__.py
      models.py

And since all the other models up until the one I had a problem with were being imported somewhere else that ended up importing from main_app which was registered in the INSTALLED_APPS, I just got lucky that they all worked.

But since I only added each app to INSTALLED_APPS and not the app_sub* when I finally added a new models file that wasn't imported ANYWHERE else, Django totally ignored it.

My fix was adding a models.py file to the base directory of each app like this...

apps/
   app/
      __init__.py
      models.py <<<<<<<<<<--------------------------
      app_sub1/
           __init__.py
           models.py
      app_sub2/
           __init__.py
           models.py
      app_sub3/
           __init__.py
           models.py
   app2/
      __init__.py
      models.py <<<<<<<<<<--------------------------
      app2_sub1/
           __init__.py
           models.py
      app2_sub2/
           __init__.py
           models.py
      app2_sub3/
           __init__.py
           models.py
    main_app/
      __init__.py
      models.py

and then add from apps.app.app_sub1 import * and so on to each of the app level models.py files.

Bleh... this took me SO long to figure out and I couldn't find the solution anywhere... I even went to page 2 of the google results.

Hope this helps someone!

Tim
  • 184
  • 5
2

I forgot to put correct arguments:

class LineInOffice(models.Model):   # here
    addressOfOffice = models.CharField("Корхоная жош",max_length= 200)   #and here
    ...

in models.py and then it started to drop that annoying

No changes detected in app 'myApp '

CodeToLife
  • 3,672
  • 2
  • 41
  • 29
2

This could be done by using two steps that are mentioned below.

  1. add your app to settings.py > INSTALLED_APPS
  2. open admin.py

from .models import upImg
# Register your models here.
admin.site.register(upImg)

NOTE: replace upImg with your className defined in models.py

after that see if there still any python manage.py makemigrations are left or not. if there is, than execute python manage.py migrate too.

For more info follow this django tutorial.

David Lee
  • 665
  • 7
  • 20
1

The solution is you have to include your app in INSTALLED_APPS.

I missed it and I found this same issue.

after specifying my app name migration became successful

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'boards',
]

please note I mentioned boards in last, which is my app name.

sradha
  • 2,216
  • 1
  • 28
  • 48
1

One more edge case and solution:

I added a boolean field, and at the same time added an @property referencing it, with the same name (doh). Commented the property and migration sees and adds the new field. Renamed the property and all is good.

kgeo
  • 412
  • 5
  • 19
1

Try registering your model in admin.py, here's an example:- admin.site.register(YourModelHere)

You can do the following things:- 1. admin.site.register(YourModelHere) # In admin.py 2. Reload the page and try again 3. Hit CTRL-S and save 4. There might be an error, specially check models.py and admin.py 5. Or, at the end of it all just restart the server

GURU RAJ
  • 21
  • 3
1

I had a similar issue with django 3.0, according migrations section in the official documentation, running this was enough to update my table structure:

python manage.py makemigrations
python manage.py migrate

But the output was always the same: 'no change detected' about my models after I executed 'makemigrations' script. I had a syntax error on models.py at the model I wanted to update on db:

field_model : models.CharField(max_length=255, ...)

instead of:

field_model = models.CharField(max_length=255, ...)

Solving this stupid mistake, with those command the migration was done without problems. Maybe this helps someone.

Yair Abad
  • 304
  • 4
  • 5
1

My problem with this error, was that I had included:

class Meta:
   abstract = True

Inside model that I wanted to creation migrate for.

Dolidod Teethtard
  • 553
  • 1
  • 7
  • 22
1

Its easy, you need to add empty init.py in empty migrations folder. Then check migrations using "python manage.py makemigrations"

Directory structure-

  • Your App
    • migrations
      • init.py
0

You should add polls.apps.PollsConfig to INSTALLED_APPS in setting.py

Derk Jan Speelman
  • 11,291
  • 4
  • 29
  • 45
Kitty
  • 1
0

The possible reason could be deletion of the existing db file and migrations folder you can use python manage.py makemigrations <app_name> this should work. I once faced a similar problem.

tafaust
  • 1,457
  • 16
  • 32
  • if after deleting existing db file, it has not worked, you can try deleting migrations folder in app folder then 'manage.py makemigrations' it worked for me! – lam vu Nguyen Nov 25 '22 at 17:26
0

If you have the managed = True in yout model Meta, you need to remove it and do a migration. Then run the migrations again, it will detect the new updates.

Irshu
  • 8,248
  • 8
  • 53
  • 65
0

The Best Thing You can do is, Delete the existing database. In my case, I were using phpMyAdmin SQL database, so I manually delete the created database overthere.

After Deleting: I create database in PhpMyAdmin, and doesn,t add any tables.

Again run the following Commands:

python manage.py makemigrations

python manage.py migrate

After These Commands: You can see django has automatically created other necessary tables in Database(Approx there are 10 tables).

python manage.py makemigrations <app_name>

python manage.py migrate

And Lastly: After above commands all the model(table) you have created are directly imported to the database.

Hope this will help.

HITESH GUPTA
  • 149
  • 4
0

Another possibility is you squashed some migrations and applied the resulting one, but forgot to remove the replaces attribute from it.

0

I had a property with the same name as the field I have tried to add with makemigrations.

wieczorek1990
  • 7,403
  • 1
  • 25
  • 19
0

Despite of many correct answers here, I yet have to add a new one as mine was a whole different thing that resulted in the same issue.

My problem was that in my models file I had my class model defined like:

class JobAd: and had forgotten to actually do it like this:

class JobAd(models.Model):

Make sure you did not do the same mistake

Rafael Santos
  • 293
  • 3
  • 18
0

Are you sure, you did not forget to inherit your class from models.Model?

-> it should be like this

MyModelClass(models.Model):
   ... rest of the code
A. L
  • 131
  • 2
  • 12
-1

First of all, make sure your app is registered in the Installed_app in the setting.py Then the above answer works perfectly fine

Arjjun
  • 1,203
  • 16
  • 15
-1

Well, I'm sure that you didn't set the models yet, so what dose it migrate now ??

So the solution is setting all variables and set Charfield, Textfield....... and migrate them and it will work.

Kid
  • 1,160
  • 2
  • 14
  • 31
Fred Lord
  • 1
  • 1