150

As the title says, I can't seem to get migrations working.

The app was originally under 1.6, so I understand that migrations won't be there initially, and indeed if I run python manage.py migrate I get:

Operations to perform:
  Synchronize unmigrated apps: myapp
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  No migrations to apply.

If I make a change to any models in myapp, it still says unmigrated, as expected.

But if I run python manage.py makemigrations myapp I get:

No changes detected in app 'myapp'

Doesn't seem to matter what or how I run the command, it's never detecting the app as having changes, nor is it adding any migration files to the app.

Is there any way to force an app onto migrations and essentially say "This is my base to work with" or anything? Or am I missing something?

My database is a PostgreSQL one if that helps at all.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
TyrantWave
  • 4,583
  • 2
  • 22
  • 25
  • Solutions offered did not work for me so here's my solution if anyone faces the same problem! 1. Delete migrations files under all apps 2. Delete database and create it again 3. run makemigrations and migrate commands P.S. Try step 1 and 3 first. If there's still an error, do steps 1-3. – Amoroso Jun 27 '18 at 04:15

36 Answers36

194

If you're changing over from an existing app you made in django 1.6, then you need to do one pre-step (as I found out) listed in the documentation:

python manage.py makemigrations your_app_label

The documentation does not make it obvious that you need to add the app label to the command, as the first thing it tells you to do is python manage.py makemigrations which will fail. The initial migration is done when you create your app in version 1.7, but if you came from 1.6 it wouldn't have been carried out. See the 'Adding migration to apps' in the documentation for more details.

drojf
  • 2,395
  • 2
  • 15
  • 9
82

This may happen due to the following reasons:

  1. You did not add the app in INSTALLED_APPS list in settings.py (You have to add either the app name or the dotted path to the subclass of AppConfig in apps.py in the app folder, depending on the version of django you are using). Refer documentation: INSTALLED_APPS
  2. You don't have migrations folder inside those apps. (Solution: just create that folder).
  3. You don't have __init__.py file inside migrations folder of those apps. (Solution: Just create an empty file with name __init__.py)
  4. You don't have an __init__.py file inside the app folder. (Solution: Just create an empty file with name __init__.py)
  5. You don't have a models.py file in the app
  6. Your Python class (supposed to be a model) in models.py doesn't inherit django.db.models.Model
  7. You have some semantic mistake in definition of models in models.py

Note: A common mistake is to add migrations folder in .gitignore file. When cloned from remote repo, migrations folder and/or __init__.py files will be missing in local repo. This causes problem.

Migration files are supposed to be included in the repo. read here. If your team frequently faces migration issues you may consider ignoring migration files as follows:

I suggest to gitignore migration files by adding the following lines to .gitignore file

*/migrations/*
!*/migrations/__init__.py

Remember, it is not recommended to gitignore migration files as per django documentation

Mohammed Shareef C
  • 3,829
  • 25
  • 35
  • 2
    I had cloned my project and migrations folder was not pushed to the repo so I had to add migrations director then I added the __init__.py and I was able to make migrations. Thanks to you – Junaid Jan 16 '18 at 08:17
  • 1
    I deleted the contents of my /migrations folder to "reset" things on a project I hadn't deployed yet. I inadvertently deleted the `__init__.py` folder along with the migrations. – Seth Apr 06 '18 at 12:27
  • 1
    This did it for me.... `You don't have __init__.py file inside migrations folder of those apps. (Solution: Just create an empty file with name __init__.py)`.. and it was caused by adding the files to `.gitignore` – lukik Jun 26 '18 at 13:18
  • For me, the issue was not declaring models in `models/__init__.py` ([docs](https://docs.djangoproject.com/en/dev/ref/applications/#how-applications-are-loaded)) – Matthew Hegarty May 07 '19 at 13:49
  • 1
    Why __init__.py file is so important in migrations folder ? to make migrations?Where Can I dig deeper for this logic? – Nimish Bansal Jul 13 '19 at 16:02
  • 1
    @NimishBansal Till python 3.3 `__init__.py` file is required inside a directory to get it treated as a python package. [see this](https://stackoverflow.com/questions/448271/what-is-init-py-for#448279) – Mohammed Shareef C Jul 16 '19 at 12:16
  • 1
    Migrations should **NOT** be gitignored! See [Should I be adding the Django migration files in the .gitignore file?](https://stackoverflow.com/questions/28035119/should-i-be-adding-the-django-migration-files-in-the-gitignore-file) – Abdul Aziz Barkat Jun 22 '22 at 16:03
  • This is a good list of issues to work through and debug – svenfulen Sep 02 '22 at 23:25
  • @Alston Django allows directories that doesn't need to have models in it. there are many scenarios in which we won't have models in every django app. Actually, this is a beginner's problem – Mohammed Shareef C Sep 20 '22 at 09:01
  • I had made a semantic mistake in defining the Model attributes. I used ":" instead of = between attribute name and field type. – smartexpert Nov 07 '22 at 20:10
31

Ok, looks like I missed an obvious step, but posting this in case anyone else does the same.

When upgrading to 1.7, my models became unmanaged (managed = False) - I had them as True before but seems it got reverted.

Removing that line (To default to True) and then running makemigrations immediately made a migration module and now it's working. makemigrations will not work on unmanaged tables (Which is obvious in hindsight)

TyrantWave
  • 4,583
  • 2
  • 22
  • 25
  • 5
    Please clarify- where did you changed/added "managed = False"? I am having the same issue – Ycon Oct 30 '15 at 06:12
  • 1
    I don't have that code anymore, but I think as a property of the class if I remember correctly. – TyrantWave Nov 14 '15 at 04:55
  • 1
    Good point. Note that `manage.py inspectdb` adds manage=False! in case you import legacy databases you must carefully tune it! – Alessandro Dentella Jan 20 '16 at 17:06
  • @TyrantWave, you saved my day. Thanks a lot. – Utkarsh Sharma May 07 '18 at 11:43
  • make sure your `app_label` is same – Luv33preet Jul 11 '18 at 11:53
  • @TyrantWave @PhoebeB thanks, this helped, but I have a question: is it really "_obvious_ _in_ _hindsight_" that changes in unmanaged models don't get picked up by `makemigrations`? It seems reasonable to expect such models to change (e.g. if the database table is changed by a third party), so why couldn't we rely on `makemigrations` when making these changes in the model? – sc28 Apr 09 '19 at 14:53
  • As I see it, `managed = False` would mean that you intend to handle everything by hand, so `makemigrations` wouldn't pick it up. – TyrantWave Apr 10 '19 at 00:40
21

My solution was not covered here so I'm posting it. I had been using syncdb for a project–just to get it up and running. Then when I tried to start using Django migrations, it faked them at first then would say it was 'OK' but nothing was happening to the database.

My solution was to just delete all the migration files for my app, as well as the database records for the app migrations in the django_migrations table.

Then I just did an initial migration with:

./manage.py makemigrations my_app

followed by:

./manage.py migrate my_app

Now I can make migrations without a problem.

Grant Eagon
  • 1,400
  • 1
  • 12
  • 24
  • FYI: it's critical that he says here, _"the files, as well as the database records."_ If you remove the database records but not the files as well (except for `__init.py__`, it will not work. – Mike Robinson Sep 19 '18 at 18:28
16

Agree with @furins. If everything seems to be in order and yet this problem arises, checkout if there is any property method with same title as the attribute which you are trying to add in the Model class.

  1. Remove method with similar name as attribute you are adding.
  2. manage.py makemigrations my_app
  3. manage.py migrate my_app
  4. Add the methods back.
Prashant Nair
  • 306
  • 3
  • 9
13

This is kind of a stupid mistake to make, but having an extra comma at the end of the field declaration line in the model class, makes the line have no effect.

It happens when you copy paste the def. from the migration, which itself is defined as an array.

Though maybe this would help someone :-)

MrE
  • 19,584
  • 12
  • 87
  • 105
Iman Akbari
  • 2,167
  • 26
  • 31
  • 1
    Your comment helped me find my issue! I didn't have a comma at the end of the last choice in a choices list..apparently Django is very touchy. – Maxim Dec 13 '15 at 02:27
  • 1
    @Maxim: that was unlikely the cause of your problem: a list without a comma at the end is still a list. Another matter are tuples: if you have only 1 element in a tuple, you need a comma after it. – blueFast Feb 07 '16 at 12:44
  • dude that saved me a lot of time! @dangonfast: in the Model definition, it is a problem indeed. – MrE Aug 11 '17 at 00:53
11

Maybe I am too late but did you try to have a migrations folder in your app with a __init__.py file in it?

Alessandro
  • 643
  • 1
  • 7
  • 20
  • 1
    If you have this "makemigrations" will create the migrations for the app. Otherwise it will require you running makemigrations app_name (which creates these file) – Scott Warren Nov 10 '17 at 05:37
7

Maybe this will help someone. I was using a nested app. project.appname and I actually had project and project.appname in INSTALLED_APPS. Removing project from INSTALLED_APPS allowed the changes to be detected.

jaywhy13
  • 1,068
  • 12
  • 16
7

The answer is on this stackoverflow post, by cdvv7788 Migrations in Django 1.7

If it is the first time you are migrating that app you have to use:

manage.py makemigrations myappname Once you do that you can do:

manage.py migrate If you had your app in database, modified its model and its not updating the changes on makemigrations you probably havent migrated it yet. Change your model back to its original form, run the first command (with the app name) and migrate...it will fake it. Once you do that put back the changes on your model, run makemigrations and migrate again and it should work.

I was having the exact same trouble and doing the above worked perfectly.

I had moved my django app to cloud9 and for some reason I never caught the initial migration.

Community
  • 1
  • 1
MicahT
  • 383
  • 3
  • 6
7

Following worked for me:

  1. Add the app name to settings.py
  2. use 'python manage.py makemigrations'
  3. use 'python manage.py migrate'

Worked for me: Python 3.4, Django 1.10

Pranshu Gupta
  • 634
  • 9
  • 13
6

People like me who don't like migrations can use steps below.

  1. Remove changes what you want to sync.
  2. Run python manage.py makemigrations app_label for the initial migration.
  3. Run python manage.py migrate for creating tables before you make changes.
  4. Paste changes which you remove at first step.
  5. Run 2. and 3. steps.

If you confused any of these steps, read the migration files. Change them to correct your schema or remove unwanted files but don't forget to change next migration file's dependencies part ;)

I hope this will help someone in future.

Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18
5

You want to check the settings.py in the INSTALLED_APPS list and make sure all the apps with models are listed in there.

Running makemigrations in the project folder means it will look to update all the tables related to all the apps included in settings.py for the project. Once you include it, makemigrations will automatically include the app (this saves a lot of work so you don't have to run makemigrations app_name for every app in your project/site).

Sticky
  • 3,671
  • 5
  • 34
  • 58
5

Just in case you have a specific field that does not get identified by makemigrations: check twice if you have a property with the same name.

example:

field = django.db.models.CharField(max_length=10, default = '', blank=True, null=True)

# ... later

@property
def field(self):
    pass

the property will "overwrite" the field definition so changes will not get identified by makemigrations

furins
  • 4,979
  • 1
  • 39
  • 57
  • A related bummer is to have a malformed field that still escapes validate/check. I defined `hourly_rate = models.DecimalField` (missing the trailing '()') and it just failed silently. – sage Sep 15 '15 at 03:58
5

Adding this answer because only this method helped me.

I deleted the migrations folder run makemigrations and migrate.
It still said: No migrations to apply.

I went to migrate folder and opened the last created file,
comment the migration I wanted(It was detected and entered there)
and run migrate again.

This basically editing the migrations file manually.
Do this only if you understand the file content.

Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41
5

Make sure your model is not abstract. I actually made that mistake and it took a while, so I thought I'd post it.

Mark
  • 18,730
  • 7
  • 107
  • 130
4

I had mistakely deleted folder of migrations from my project directory.

Solution is to create __init__.py file in the migrations folder, and then,

python manage.py makemigrations
python manage.py migrate
Walk
  • 1,531
  • 17
  • 21
3

Did u use schemamigration my_app --initial after renaming old migration folder? Try it. Might work. If not - try to recreate the database and make syncdb+migrate. It worked for me...

Alex Vidis
  • 49
  • 8
  • 10
    No command `schemamigration` exists - I think that's part of South? I don't have a migration folder at all currently. Removing my `models.py` and rerunning `inspectdb` didn't seem to do anything. – TyrantWave Jul 23 '14 at 14:44
  • 2
    `schemamigration` was from South. `makemigrations` is its replacement. – Craig Labenz Jan 12 '15 at 23:32
  • 2
    This is still valid. But it changed to `makemigrations --empty` – Iulius Curt Oct 08 '15 at 10:50
3

In my case I needed to add my model to the _init_.py file of the models folder where my model was defined:

from myapp.models.mymodel import MyModel
robert wallace
  • 379
  • 4
  • 7
2

Had the same problem Make sure whatever classes you have defined in models.py, you must have to inherit models.Model class.

class Product(models.Model):
    title = models.TextField()
    description = models.TextField()
    price = models.TextField()
Sonu Kumar
  • 37
  • 5
1

I had the same problem with having to run makemigrations twice and all sorts of strange behaviour. It turned out the root of the problem was that I was using a function to set default dates in my models so migrations was detecting a change every time I ran makemigrations. The answer to this question put me on the right track: Avoid makemigrations to re-create date field

PhoebeB
  • 8,434
  • 8
  • 57
  • 76
1

I recently upgraded Django from 1.6 to 1.8 and had few apps and migrations for them. I used south and schemamigrations for creating migrations in Django 1.6, which is dropped in Django 1.8.

When I added new models after upgrade, the makemigrations command wasn't detecting any changes. And then I tried the solution suggested by @drojf (1st answer), it worked fine, but failed to apply fake initial migration (python manage.py --fake-initial). I was doing this as my tables (old tables) were already created.

Finally this worked for me, removed new models (or model changes) from models.py and then had to delete (or rename for safety backup) migrations folder of all apps and run python manage.py makemigrations for all apps, then did python manage.py migrate --fake-initial. This worked like a charm. Once initial migration is created for all apps and fake initial migrated, then added new models and followed regular process of makemigrations and migrate on that app. The changes were detected now and everything went fine.

I just thought of sharing it here, if someone faces same problem (having schemamigrations of south for their apps), it might help them :)

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
1

Maybe that can help someone, I had the same problem.

I've already created two tables with the serializer class and the views. So when I wanted to updated, I had this error.

I followed this steps:

  1. I made .\manage.py makemigrations app
  2. I executed .\manage.py migrate
  3. I erased both tables of my models.py
  4. I erased all reference to my tables from serializer and view class.
  5. I executed step 1 and 2.
  6. I retrieved my changes just in the models.py
  7. I executed again step 5.
  8. I restored all my changes.

If you're working with Pycharm, local history is very helpfull.

fingerprints
  • 2,751
  • 1
  • 25
  • 45
1

Maybe this will help someone.

I've deleted my models.py and expected makemigrations to create DeleteModel statements.

Remember to delete *.pyc files!

Sebastian Wagner
  • 2,308
  • 2
  • 25
  • 32
1
./manage makemigrations
./manage migrate

Migrations track changes to DB so if youre changing from unmanaged to managed, you'll need to make sure that youre database table is up to date relating to the Model you're dealing with.

If you are still in dev mode, I personally decided to delete the migration files in my IDE as well as in the django_migrations table relating to my Model and rerun the above command.

REMEMBER: if you have a migration that ends with _001 in your IDE & _003 in your database. Django will only see if you have a migration ending with _004 for anything to update.

The 2 (code & db migrations) are linked and work in tandem.

Happy coding.

A H Bensiali
  • 825
  • 1
  • 9
  • 22
1

You may need to fake the initial migrations using the command below

python manage.py migrate --fake-initial
dtar
  • 1,469
  • 12
  • 17
1
  1. Remove changes what you want to sync.
  2. Run python manage.py makemigrations app_label for the initial migration.
  3. Run python manage.py migrate for creating tables before you make changes.
  4. Paste changes which you remove at first step.
  5. Run 2. and 3. steps
0

Added this answer because none of other available above worked for me.

In my case something even more weird was happening (Django 1.7 Version), In my models.py I had an "extra" line at the end of my file (it was a blank line) and when I executed the python manage.py makemigrations command the result was: "no changes detected".

To fix this I deleted this "blank line" that was at the end of my models.py file and I did run the command again, everything was fixed and all the changes made to models.py were detected!

Art_Code
  • 518
  • 4
  • 12
  • Well in django 2.0 + that empty line is required i believe , i had to do the opposite of what you did buddy – Sumit Kumar Saha Feb 17 '18 at 12:37
  • @SumitKumarSaha haha I am using currently Django 1.7 version and that blank line was the reason of 2 hours trying everything to solve the migration error. Thanks for sharing Sumit. Have a nice day – Art_Code Feb 17 '18 at 13:36
0

First this solution is applicable to those who are facing the same issue during deployment on heroku server, I was facing same issue.

To deploy, there is a mandatory step which is to add django_heroku.settings(locals()) in settings.py file.

Changes: When I changed the above line to django_heroku.settings(locals(), databases=False), it worked flawlessly.

Rishabh Aher
  • 61
  • 2
  • 7
0

I have encountered this issue, the command

python manage.py makemigrations

worked with me once I saved the changes that I made on the files.

0

One of the cause may be You didn't register your models in admin.py file . First register your models in admin.py file then do the migrations.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 23 '21 at 07:57
  • this is a relevant case. for models we declare to be identified by django, we need to use it somewhere somehow – Mohammed Shareef C Jun 28 '22 at 08:59
0

Load ur app to settings.py and py .\manage.py makemigrations

INSTALLED_APPS = [
    'appname'
]
Thiwanka
  • 91
  • 1
  • 3
0

for me i remove migrations files on env folder env\Lib\site-packages\django\contrib\auth\migrations\... but keep the __init__.py don't remove it

Taha Omar
  • 51
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 15 '22 at 12:20
0

sometime/somehow you get into situation where django understands that migration is already applied and when you run migrations it will say "No changes detected" in this situation follow these steps:

  1. delete the last migration from your migration table

    delete from django_migrations where id =

  2. delete you last migration file from migrations folder enter image description here

  3. run the migrations and it should work

    django-admin manage.py makemigrations django-admin manage.py migrate

shyam yadav
  • 214
  • 1
  • 10
0

First make sure that your app is registered on

INSTALLED_APPS

of your project settings.py file. If your migrations don't apply to your project you can run

python manage.py makemigrations {app_name}

then apply to migrate the command

python manage.py migrate

or

python manage.py migrate {app_name}

if migrations are applied but migrate command is not applied, check your database, there will be a table called "django_migrations". Then check whether your newly added migrations file exists in this table or not. If exists remove this row and apply to migrate command again. Hope it will work.

PLABON DATTA
  • 167
  • 1
  • 4
-1

Adding my 2c, since none of these solutions worked for me, but this did...

I had just run manage.py squashmigrations and removed the old migrations (both the files and lines in the the django.migrations database table).

This left a line like this in the last migration file:

replaces = [(b'my_app', '0006_auto_20170713_1735'), (b'my_app', '0007_auto_20170713_2003'), (b'my_app', '0008_auto_20170713_2004')]

This apparently confused Django and caused weird behavior: running manage.py makemigrations my_app would recreate the initial migration as if none existed. Removing the replaces... line fixed the problem!

webtweakers
  • 715
  • 7
  • 19
-1

python manage.py makemigrations accounts Migrations for 'accounts': accounts\migrations\0001_initial.py - Create model Customer - Create model Tag - Create model Product - Create model Order

Note: here "accounts" is my app name