Django 1.7 - create initial migration - django

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.

Related

Django makemigrations No changes detected in app

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

Django proxy model cannot be migrated when upgrading 1.4 -> 1.7

Having a model like below
from django.contrib.auth.models import User # has migrations in 1.7
class MyProxyUser(User):
class Meta:
proxy = True
in an app without migrations gives the following error when running the tests:
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'django_proxy_model_problems.MyProxyUser'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more
Having read both
https://docs.djangoproject.com/en/1.8/topics/migrations/#dependencies
https://docs.djangoproject.com/en/1.8/topics/db/models/#proxy-models
I can't figure out what I should be doing to resolve this problem (manage.py makemigrations reports No changes detected)
The problem can be reproduced by running tox -e py27-django17 using this github repo
This can be reproduced with Django 1.7 & 1.8, but works fine with 1.4
Run manage.py makemigrations <app_label>. Django won't create a migration folder if it doesn't exist, unless you explicitly specify the app label. That is because apps without migrations are (still) supported.
If the migrations folder exists within the app, and has an __init__.py file (even on Python 3), Django will pick up the app as a migrated app, and create the migrations with just manage.py makemigrations.

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 on Heroku: how do I get makemigrations to rescan the database?

I have Django 1.7 running on Heroku. I've made a change to the models.py file (added a column to a table) but Django doesn't seem to be able to detect this. When I run
python manage.py makemigrations appname
it responds No changes detected in app.
I've tried deleting the appname/migrations folder, but that doesn't help.
Is there a way to get Django to rescan the database and check for differences? This was easy with South.
https://docs.djangoproject.com/en/1.7/topics/migrations/#the-commands
Have you tried
python manage.py migrate
It seems the migrate is "responsible for applying migrations, as well as unapplying and listing their status."

django south migration - app migration is not in south_migration table

I have added a new app to my django application. The app has its migration scripts. When I run migration it says nothing to migrate. But when I look at the south_migration table it doesn't have a record. Yet all the other apps have record.
I also see database is acting bit strange. What do I need to do to get south_migration to have a record of my apps migration.
Make sure you got this particular app in INSTALLED_APPS.
Make sure app has folder and file migrations/__init__.py and of course migrations. (It seems that you've done that)
Also you can try resetting the app by doing (careful, it will remove tables in db):
./manage.py migrate app_name zero
To answer your last question: South saves migration history in db only after you run:
./manage.py migrate app_name