Django makemigrations No changes detected in app - django

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.

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.

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):
...

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

Related

makemigrations doesn't detect changes in model

I'm using django 1.9.6. I recently deleted my migrations and ran migrate --run-syncdb and makemigrations my_app. Today I added a new field to one of my models:
models.py:
value = models.PositiveSmallIntegerField(null=True)
I tried to migrate the changes, but makemigrations doesn't detect the change. It's just the development version, so I can resync (I don't have to preserve the data), but running --run-syncdb again doesn't detect it either.
Why isn't this migrating?
Delete every past migration files and __pycache__ files except __init__
then:
python manage.py makemigrations yourApp
After that make sure that the db is the same as the code in model.py (remove new changes) and run next line:
python manage.py migrate --fake-initial
Now add all your changes in the model.py and run next lines:
python manage.py makemigrations
python manage.py migrate
Best Regards,
Kristian
I had the same issue. I realised I had a property defined on the model with the same name as a field I was trying to add on the model. Ensure the model doesn't have a model property/method with the same name as the field you are trying to add.
You should not delete migrations, you should squash them. If you simply deleted the files you likely messed things up, the easiest way to recover is re-sync your code to get the files back. A more complex route is to delete all the records from the django_migrations table and re-init the migrations from scratch but there is more steps/issues than I can really get into and I don't recommend it.
The reason makemigrations is not detecting the change is likely because there is no migrations folder in that app. If you run python manage.py makemigrations your_app --initial it might detect and generate the migrations or it might freak out because of the difference in your files and the django_migrations table.
The --run-syncdb command is great when you don't care about the data, usually before you actually deploy but once you start using migrations you should not use the --run-syncdb command anymore. For instance, during initial development here is the code I run every model change instead of dealing with migrations:
dropdb mydb && createdb mydb && python manage.py migrate --run-syncdb && python manage.py loaddata initial
I store all the initial data in a fixtures file and the command wipes out the entire database, the --run-syncdb rebuilds the schema, and the initial data is loaded while skipping actual migration files.
So, if you don't care about any of your data or can easily move it to a fixture than you can drop and re-create the DB. You are then free to delete all migration folders and you can use the command above until you go live and need to move to using migrations.
*** UPDATE FOR DJANGO 1.11 *** STILL USING ON 3.2.5 ***
I started using Django 1.11 and noticed the test framework can fail if you have dependencies on framework models and don't actually have migrations. This is the new command i'm using to wipe everything out and start over while still developing.
set -e
dropdb yourdb
createdb yourdb
find . -name "migrations" -type d -prune -exec rm -rf {} \;
python manage.py makemigrations name every app you use seperated by space
python manage.py migrate
python manage.py loaddata initial
I put this in a builddb.sh at the root of my project (next to manage.py) so I can just run ./builddb.sh. Be sure to delete it on deploy so there is no accidents!
Try creating a migrations folder that contains an empty __init__.py file inside your app folder if there is no migrations folder already. And make migrations.
In my case, I created a new project and created an app. The python manage.py makemigrations returned no change detected although I had added a handful of models.
Found out that you need to manually add the name of your app in the INSTALLED_APPS list inside your settings.py file. Unless you do that, no change in the migration of that app can be detected.
If you're deleting a model, and expecting the changes to be picked up in migrations, ensure that the model doesn't have a *.pyc still lying around.
Another case is abstract classes. Make sure that your model’s metaclass does not have the abstract = True property.

Django makemigrations wants to delete everything it just created

I just followed this procedure:
makemigrations (success)
migrate (success)
Copy the app on another server (with the migration files)
Create a new empty database on that server
migrate (success, it creates the correct schema)
Fill the new database with data
Just to test: migrate ....
At this point Django says I have "changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them"
But when I run makemigrations, it creates a new one that wants to "Remove field" every foreign key and "Delete model" all of my models. If I run it it empties my database. My models.py are intact.
What is happening ??
I had the same problem in my project. Running forward I can say that django removes models that has no import (making the Delete model migration). The documentation says that you should import your model in the myApp/models/__init__.py file (see https://docs.djangoproject.com/en/1.11/topics/db/models/#organizing-models-in-a-package).
In my case I imported the model somewhere to make manipulations but model had been removing elsewhere.
I had made a useless import in an admin.py file that has solved my situation (I didn't try to follow the documentation and import it in the __init__.py but sure it should help).
I had not realized yet why does it work that way (hope that someone could note this moment) and also hope this solution will help you.
I've just had the same errors, but I hit the makemigrations building a migration full of Remove Field and Delete Model commands before I tried the migrate command.
In my case, the solution was related this being an old project I was resurrecting, and it had a number of Model.Meta.app_name values set, as well as entries in apps.py for each project. These were now in conflict with the way settings was interpreting the project, and even though my models were imported into views and admin, they weren't being seen by the migration code. Deleting these app_name tags on the models, and modifying the AppConfig name in apps.py solved this, so that the running makemigrations again caused the expected changes (alter field, etc), and all was fine.
I didn't find any other questions or answers that quite matched this one and my experience, so I hope if anyone else looks for this, they'll find it here like I did ;-)

Django: I get a [relation "auth_group" does not exist] error after syncdb

I started a new Django 1.8 project and realized that I missed something (i had done the initial migrations). I dropped the database (postgreSQL) and deleted migration folders from all my apps in order to start from scratch.
Now, when I 'syncdb' I get this error:
django.db.utils.ProgrammingError: relation "auth_group" does not exist
and when I makemigrations I get this:
No changes detected
What am I doing wrong?
Probably you should try to create migrations modules (folders named migrations with empty file named __init__.py inside of each directory) for your apps. And then run manage.py makemigrations again.
The problem is on no changes detected. Please execute these commands with your app name. I guess you didn't add it (just like the mistake I did):
python manage.py makemigrations myappname
python manage.py migrate myappname
The above error occurs when you have django.contrib.admin among the installed applications.
Run these commands in their respective order.
**
./manage.py makemigrations
./manage.py migrate auth
./manage.py migrate**
That worked for me perfectly.
Doing ./manage.py migrate auth first didn't work for me, and every ./manage.py command was throwing this error. My problem was that I was doing stuff with the Group manager in module scope.
If you have code like this in module scope:
customers_group = Group.objects.get(name='customers')
Move it inside a function that is called at runtime instead.
def xyz():
...
customers_group = Group.objects.get(name='customers')
I had the similar problem with Django2.2 migrations. I will post what helped in case someone is looking to fix this.
I commented out all urls to apps(like my_app.urls, your_app.urls) in main project urls.py and then ran makemigrations, it worked.
I think this error is due to some forms/views referring to model/fields that are not yet created. It seems django traverses urls.py to before making migrations
It can be either:
one of the pip dependencies from requirements.txt was using South
had this error when running tests which do migration in Django 1.8. Found the lib with issue by running tests in verbose mode. Consider upgrading the library to newer version.
manage.py test -v 3
one of the /migrations folder might still has old South migrations files.
It can be because others are still adding migrations when you are trying to upgrade Django. Use the following to make sure that the expected migrations files are present in each app.
manage.py showmigrations
One of your paths ("pointing urls.py on your core folder along with the settings.py") makes that problem occur importing django.contrib.auth and directly using methods and properties of "auth" after calling those views
Remove all migrations except "init.py" of each apps
Go to projects urls.py and comment out all the paths
run "heroku run python manage.py makemigrations"
run "heroku run python manage.py migrate"

Django 1.7 - create initial migration

According to the Django docs, if I want to create an initial migration for an app, I should do:
$ python manage.py makemigrations my_app
However, if I do that in my project, I get:
No changes detected in app 'my_app'
even though there are no migrations for my_app yet - the my_app/migrations/ folder only has an __init__.py file.
I do NOT have managed = False in my model. The model classes in question don't even have a Meta class defined. What else can prevent Django from detecting model changes?
How does Django detect if/when there are changes?
Update:
I should add that migrations for this particular app worked fine back when I was using South migrations. It's only after upgrading to Django 1.7, and built-in migrations, that it can no longer figure out if/when there are model changes for that particular app (migrations for other apps work fine).
A little late, but having just hit this after creating a brand new app, it suddenly dawned on me that I hadn't added the new app to the INSTALLED_APPS in the settings.py.
INSTALLED_APPS = (
...
my_app,
...
)
Doing that and then re-running python manage.py makemigrations my_app generated the initial migration.
You might want to look for a "migrations" directory somewhere in your virtualenv home directory or on your path.
I ran a few times into some similar issue, when trying to migrate an app from South to Django 1.7 migrations. For some reason, Django wouldn't find the correct migrations folder, and so would create the migration into an unlikely location such as <virtualenv>/bin/myapp/migrations dir (when using django-admin.py). So everytime I'd run makemigrations Django would find this "stale" migration, and display the No changes detected in app 'my_app' message.
Sorry if I'm vague on the specifics, I'll update next time I run into this issue.

How to remove models from django?

In Django how do you remove models that you have synced into the database?
For example in Django tutorial page had this following code
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
Then I used python manage.py sql polls and python manage.py sql choice to create the tables into the database. But what if I did something wrong and don't want that model any more. What's the syntax to remove it?
If you don't want to delete and re-sync your current database, the best way is to drop the table from your model manually:
$ python manage.py dbshell
> (Inside the DB shell)
> DROP TABLE {app-name}_{model-name};
Why not simply try deleting the models from your models.py file? When you run
python manage.py makemigrations
the migrations file should be updated with the deleted models.
There is no syntax. Django doesn't removes tables or columns. You have to manually change your database or use a migration tool like South.
If you justing playing around with tutorial the easier way is to delete your sqlite database file and run a sync again.
Commenting out the class that defines the model did it for me. Once I had done it and ran python manage.py makemigrations,
I got this as response:
- Delete model MyModel.
Checked afterwards with a DB Browser and it was actually removed.
The most easiest solution is to just delete your model from models.py and run
python3 manage.py makemigrations
(Note: Remove the model from everywhere where you have imported it like admin.py, views.py, or any other file where you have imported it)
If you are facing issue to update changes onto DB so you can directly run this command.
python manage.py migrate --run-syncdb
I found a simpler method, by sheer experimentation. Even after deleting tables, Django was not making the migrations, so I did the following:
Simply delete the files created in your myapp->migrations directory, making sure that you do not delete the init.py and pycache
Starting from 001initial.py and downwards delete the files.
Run python manage.py makemigrations
Run python manage.py migrate
-M
Django’s database handling through syncdb is purely additive: any new models will be added, but deleted models will not be deleted and modified models will not be modified.
If you do not have any data you want to preserve, you are fine just dropping and recreating the database: if you have anything you want to preserve, or even if you intend to have anything you want to preserve, I cannot advise you strongly enough to use a migration tool: South has been the de facto standard for every project I’ve worked on.
Since the Migration command handle Model(database) you can do following steps.
First type
python manage.py makemigrations app_name # it will restructure your model
then type
python manage.py migrate app_name # it will apply to restructure your database.
Example:
I had Posts and PostDetail model,
later on, I wanted to remove PostDetail model and some fields(columns) from Posts model too.
I simply run migrations and migrate commands,checked in Mysql Database. It worked fine.
Hope it will work for you too.
Weather you’re removing a single model or a full app, you should first
remove the desired models from the models.py file.
Moreover, you have to make sure that no other file imports these
models or uses them (admin.py, views.py, etc).
Next, run South or migrate your database to properly delete these
models from your database.
Check the source of this information on the link below:
http://www.marinamele.com/how-to-correctly-remove-a-model-or-app-in-django