4

I have trouble with my makemigrations command.

Note: I have successfully make migrations till now, so it is not the first time I try to make migrations on this project.

I have my project in INSTALLED_APPS.

Problem: For some reason project stop detecting any changes in my models.

Inside my project models.py I have:

from myproject.myfolder import myModel1
from myproject.myfolder import myModel2
from myproject.myfolder import myModel3

if a add new models as myModel4 class and import it inside models.py and I try to

python mamange.py makemigrations environment=local

I get No changes detected

I know there are a lot of posts of making the initial migrations, so I even try

python manage.py makemigrations myproject environment=local

I even try to delete all files in __pycache__ but it doesn't work for me.

I even try to delete database and create new one, and it doesn't work either.

EDIT:

Because I delete the database and make it new again, database is empty, but I still get same message.

Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
  • 1
    Why `environment` ? Is `myProjectLabel` in your settings INSTALLED_APPS ? – user2021091 Oct 17 '17 at 11:41
  • yes it is. As I wrote, it is not the first migration. The tag environment is for different stages of deployment. Settings are wrote (like database) for each stage differently – Marko Zadravec Oct 17 '17 at 11:47
  • Sorry if it was confusing. I just try to avoid suggestions like : you need to write name of the app inside apps.py. In other posts this was very common. I edit post. – Marko Zadravec Oct 17 '17 at 11:58
  • Have you set `app_label = 'myproject'` for the new models you are importing into `myproject/models.py`? – Alasdair Oct 17 '17 at 12:09
  • Hummm. Your imports are weird. So you define class Foo(models.Model) outside models.py, right? Try: from myApp.myModelFile import Foo. For this to work you need myModelFile in the same level as models.py. If it is in a folder you should put an empty __init__.py file there and: from myApp.myFolder.myModelFile import Foo. – Alex Oct 17 '17 at 12:57
  • @Tico all imports are ok. We have git repository, and same code on other machines are working. And as I say in my problem, this is running project. I did a lot of migrations already, same way. I assume there was some error or something with my last migration, and I need to restart/reset/,.. something so that code will work again – Marko Zadravec Oct 17 '17 at 14:07
  • Right! Another shot. You said you deleted the database. If you go in psql(or whatever) and drop database yourdatabase; then create database youdatabase, Then delete all files in folder migrations (except __init__.py) it MUST work. If that does not work, you probably have django installed more than once and running the incorrect one. Do you use virtualenv? Or have another django installation on the same server. – Alex Oct 17 '17 at 14:14
  • Sorry @Tico, I have to disappoint you. It doesn't work. I drop the table, create new table and add all permission to old user to this new database. I delete all migrations files (except __init__) and run "makemigrations myproject" but it still No changes detected in app. I use virtualenv, and I use same one as I used before. I could make another one and install all libraries inside if you think that would help. I will look my mind, because as you said it SHOULD work :D – Marko Zadravec Oct 17 '17 at 15:55
  • One new information. I looked in history what commands I run today (in the morning migrations was working) the only unused command until now was "python manage.py migrate --run-syncdb". This was my unsuccessful attempt to fix the problem. Could this be the reason it doesn't work? – Marko Zadravec Oct 17 '17 at 16:02
  • Humm... Are you absolutely sure that manage.py can see you app? Do you have a view that works, or can you run ./manage.py test myapp to check if the app is being seen? syncdb is for old django versions (before 1.9) and I don't know what parameter --run-syncdb does. Maybe you are facing legacy conflicts? I'm running out of suggestions... :/ – Alex Oct 17 '17 at 16:23
  • @Tico, Yes unit test runs normally (if I delete those who use new database object). – Marko Zadravec Oct 17 '17 at 16:36
  • To be more mysterious I pull data fresh data from git, drop table, create new virtual environment and still doesn't work – Marko Zadravec Oct 17 '17 at 17:22
  • make sure you create your model from models.Model like class X(models.Model): – aysebilgegunduz Aug 19 '19 at 12:56

3 Answers3

2

I just ran into an issue like this. In my case, the problem was that I had installed, through pip, the stable version of the package that I was developing, and Django was importing the stable version rather than my development version. To check if this is the case with you, try adding a syntax error to models.py. If makemigrations doesn't trigger the syntax error, then you'll know that your version is not even being loaded by the python interpreter.

timthelion
  • 2,636
  • 2
  • 21
  • 30
  • This was my issue - it's even more likely to occur if you're using docker, like I was. – BDuelz Nov 01 '21 at 00:51
  • @BDuelz How does Docker trigger this? – timthelion Nov 01 '21 at 15:26
  • Well I was running the proper migration commands, but I failed to `docker build` so my docker containers were not up to date with the correct migration code. Simple oversight, but one that robbed me of a few hours before I noticed. – BDuelz Nov 11 '21 at 16:25
1

If your model is not inheriting from django model then, you will see aforementioned error. Make sure that your custom model inherits from django models.Model, something like this.

from django.db import models

class Posts(models.Model):
    ...
Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124
-3

Deleting the DB and creating new one will never work since it refer the previous migration files. Delete all previous migration files and pycache files except init. Then try running these.

    python manage.py migrate --fake-initial
    python manage.py makemigrations
    python manage.py migrate

This worked for me

SUP
  • 349
  • 1
  • 2
  • 11
  • 1
    migrations are stored in the database, removing it django will reapply all the migrations. you don't need to delete migrations files. – Yusef Maali Oct 17 '17 at 13:07
  • 1
    Nop, this is not working. I already try this. When I do migrate --fake-initial it says to me : No migrations to apply – Marko Zadravec Oct 17 '17 at 14:05