Create migrations for models inside egg dependency - django

I have a Django project that itself does not have apps. All apps come in through egg dependencies installed in a pyvenv environment.
Those apps have models but do not have 'manage.py' or database settings (just a plain app).
I am now struggling to create the migrations for the apps in the eggs. When I execute 'python manage.py makemigrations' I get 'No changes detected' even though I wiped the DB before. When I then run the server it tells me that I have 13 unapplied migrations from Django core modules such as 'auth', 'sessions' etc. I can apply them running 'python manage.py migrate'.
I tried creating a dummy app, added it to INSTALLED_APPS and added an import of a model from an egg to models.py of that app. Didn't work either, still 'No changes detected'.
Those egg dependencies are apps I created. Is 'egg' the wrong format here? What are the alternatives? Can I tell the 'makemigrations' module where to look? What else could be the cause?

Thats because Django is looking for directories when looking for migrations. Eggs are not directories therefore will not find migrations for the apps installed as eggs.
If you tell makemigrations to do it for you, you will see an error like (on windows):
FileNotFoundError: [WinError 3] The system cannot find the path specified: C:\\path\\to\\app_egg.egg\\app\\migrations
I think the best solution is to not use eggs.

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

Django South problem in Aptana

I try to add South migration tool to my Django app. I installed South by running python setup.py install and it is installed successfully. Now I can run migrate appname and schemamigration appname --auto commands and they work great.
However, after I run migration appname, it created migration package under my app folder and created a init.py. There are such imports in this init file
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
Although everything works perfect, from south.db and from south.v2 lines give error in Aptana which is about it cannot import these files.
Do you have any idea why it happens ? Should I add the location of south folder to any system path ?
Thanks
Have you added the parent directory where south was installed (site-pacakges, or maybe a virtualenv's site-packages) folder to your Project's pythonpath?
You'll need to set your project as either a PyDev project or Django project.
Right-click on your project, choose the correct nature.
Then right-click again and go to Properties.
Choose the PyDev-PYTHONPATH setting and click on External Libraries.
Click Add source folder, nav to the parent of the module you want to add to the path and hit either apply or OK, depending on how many folders you want to add to the project's pythonpath.
Hope that helps you out.

Django: How to completely uninstall a Django app?

What is the procedure for completely uninstalling a Django app, complete with database removal?
Django < 1.7 has a handy management command that will give you the necessary SQL to drop all the tables for an app. See the sqlclear docs for more information. Basically, running ./manage.py sqlclear my_app_name gets you get the SQL statements that should be executed to get rid of all traces of the app in your DB. You still need to copy and paste (or pipe) those statements into your SQL client. For Django 1.7 and up, use ./manage.py migrate my_app_name zero (see the migrate docs), which runs the database cleaning automatically.
To remove the app from your project, all you need to do is remove it from INSTALLED_APPS in your project's settings.py. Django will no longer load the app.
If you no longer want the app's files hanging around, delete the app directory from your project directory or other location on your PYTHONPATH where it resides.
(optional) If the app stored media files, cache files, or other temporary files somewhere, you may want to delete those as well. Also be wary of lingering session data that might be leftover from the app.
(optional) I would also remove any stale content types.
Like so.
from django.contrib.contenttypes.models import ContentType
for c in ContentType.objects.all():
if not c.model_class():
print "deleting %s"%c # print(f"deleting {c}") # for Python 3.6+
c.delete()
In my context the projects exists several times: I have a development system, some team mates have a development system, there is a staging system for the customer and a production system. This means I don't want to execute sql commands by hand. I want it to be automated.
Goal: Remove the app and all database tables.
Step 1: empty the app, but leave it installed
remove all files from the app, except the folder "migrations"
Execute this command:
python manage.py makemigrations -n drop_all_tables my_app_to_remove
The directory looks now like this:
my_app_to_remove/
my_app_to_remove/__init__.py
my_app_to_remove/migrations
my_app_to_remove/migrations/0001_initial.py
my_app_to_remove/migrations/....
my_app_to_remove/migrations/0030_drop_all_tables.py
my_app_to_remove/migrations/__init__.py
Leave my_app_to_remove in the file "settings.py".
Step 2: Deploy the changes
Update all projects. Tell the team mates to update their project and to run the migrations.
Step 3: remove "my_app_to_remove" from settings.py
Now remove "my_app_to_remove" from settings.py and deploy again.
comment out on settings.py in INSTALLED_APPS unnecessary app's line
delete all folder __pycache__ and migrate at your project
delete unnecessary model in models.py
delete all import link in views.py, admin.py end etc.
delete all link's in urls.py on your unnecessary app's
in database delete unnecessary tables wich associated with the app (I do it with help program "Valentina Studio")
delete app's folder
in command line do it: python manage.py migrate and python manage.py syncdb
django app is a "set" of *.py files and a directory with a django-app-name. So you can simply delete the whole folder with all *.py files
To "remove" tables from DB you should use DELETE FROM <app-name_table-names>
Furthermore, you have to delete lines witgh app-name from setting.py in a root directory
As a first step, prevent any usage of the app models and deploy. This is essential for rolling deployment. Check notes below.
Then you have two options.
Option 1
(Manual) Run python manage.py migrate <app_name> zero. This will revert all the migrations for the app and clean the django_migrations table
Remove app (code, INSTALLED_APPS, urls.py, etc.)
Deploy (python manage.py migrate)
Option 2
Remove all models in the app
Deploy
Remove the app (code, INSTALLED_APPS, urls.py, etc.)
Deploy
(Manual) Clean django_migrations table
This answer is thinking in an automated rolling deployment context (e.g. Heroku).
Manual means it normally cannot be done in automated deployment. Deploy basically refers to python manage.py migrate which is normally done in automated deployment.
Notes
Make sure to remove code importing this app from other apps. Also any references in settings.py (but keep it in INSTALLED_APPS so we can run migrations), urls.py, etc.
migrate <app_name> zero will revert all migrations depending on those migrations. So be careful if your other apps migrations depend on these migrations.
Also be aware of cascade delete if you have not just schema migrations but also data migrations.
Signal receivers receiving signals defined in other apps might be an issue in rolling deployment.
References
https://docs.djangoproject.com/en/stable/ref/django-admin/#migrate
Safely Remove a Django app by Jordan Haines
To completely remove a Django app (with models), follow the steps below. Let’s pretend we’re removing an app called note_app:
Search through your other apps for any note_app imports. The easiest way to do this is a project-wide search for “from note_app”. You may want to exclude the note_app directory from your search.
Comment out all models in note_app.models. You may need to also remove entries from note_app.admin file if you registered your note_app models with Django admin.
Look for and resolve errors when trying to run your Django app. P.S. You may have missed some model imports in step 1.
Note that depending on how you define ForeignKey, OneToOne and ManyToMany fields, you may have missed some keys to your note_app models in step 1. It’s important that any fields that point to note_app models from models in other apps need to be deleted before you continue. Take a minute to make sure none of these fields were missed; if they were, then delete those fields that remain and create migrations that will remove the fields from your database.
Run makemigrations. This should create a note_app migration that deletes all of the models you just commended out. This migration should also remove fields referring to note_app models from models in other apps.
Run your migrations. You must run your migrations in all environments (including production) BEFORE you delete the app directory. The migrations that remove your app’s models from the database will be — surprise — in your note_app directory. If you delete the app directory prematurely, you will delete these migrations before they have a chance to clean up your database.
You may get a notice that the content types for your deleted models are stale. When asked whether or not you want to delete these content types, reply “yes”
Git Tip: Commit your migrations, take note of the commit, and then create a separate commit that deletes the note_app directory. When you are ready to apply your changes in a staging or production environment, checkout the commit you noted, run the migration, and then checkout the latest commit to delete the app directory. Your first commit should still have note_app in INSTALLED_APPS.
Delete the directory that contains the note_app.
Remove note_app from your INSTALLED_APPS setting.
And that’s really about it…super easy :)