South skip broken migrations - python

I am using a 3rd party app inside my django application and the older versions of it had a dependancy on the django auth model, but the newer version supports the custom auth model of django 1.5.
The problem I am having is that when I install the app and migrate app, it breaks on the migration 002 because it is referencing a table that the final version of the app doesn't need, therefore i dont have.
If i turn off south and just do a syncdb everything works fine. But then I will have to do fake migrations for all my other apps. Is there an easy way that I can have either south skip these errors and keep proceeding with the migrations or south just use the models.py to create the schema and then for me to do a fake migration for just that one app?
Thanks for your help :)

I ended up doing this
SOUTH_MIGRATION_MODULES = { 'app_with_broken_migration':'ignore'}
then running syncdb
then removing the ignore migrations line and running
manage.py migrate app_with_broken_migration --fake

I usually make a temporary modification to the migration script that fails. Comment out or modify the parts that are not needed, run the migrations, then restore everything to the way it was before.
It's not ideal, and it involves some duplication of work - you have to do the same steps both on dev machine and on the server, but it lets you preserve South support and work around the failing migration.

Related

Django-Cms and Django-shop migration issues

I started creating my website on my own PC using the Django-cms install script then I tried to add a lot of applications such as Django-Helpdesk and others and everything was smooth until I tried to add pieces of djangoSHOP demo project and dependencies to email_auth.
Then I started to have report that the migration history was inconsistent and migrations applied out of order.
Before adding djangoShop to the installed apps, I just add the following commands to setup the DB:
manage.py migrate --run-syncdb --noinput
manage.py migrate --noinput
and everything was ok.
Now, as I'm just starting, I don't have changes to the schema to apply but most of the django apps I try to install have migrations folders with several migrations.
I don't want to update an existing schema, I want to create a new one.
I'm working from a blank database so do I really need to apply all those migrations ? To me it seems that the default behaviour of running migrations is not compatible with starting from a blank db,
I don't understand what to do just initialise the database to just have the required tables created.
Did anyone got the same kind of problems?
How did you fixed it?
Edit:
django-cms>=3.5,<3.6
Django 1.11.19
Error message:
Django InconsistentMigrationHistory: Migration Helpdesk.000xxxxx.py is applied before its dependency email_auth.0001_initial.py on database 'default'

What's the right way to handle an initial database migration in Django?

I'm in the process of preparing a Django application for its initial production release, and I have deployed development instances of it in a few different environments. One thing that I can't quite get happening as smoothly as I'd like is the initial database migration. Given a fresh installation of Django, a deployment of my application from version control, and a clean database, manage.py migrate will handle the initial creation of all tables (both Django's and my models'). That's great, but it doesn't actually create the initial migration files for my apps. This leads to a problem down the road when I need to deploy code changes that require a new database migration, because there's no basis for Django to compute the deltas.
I've tried running manage.py makemigrations as the first step in the deployment, in the hopes that it would create the migration files, but it reports that there are no changes to migrate. The only way I've found to get the baseline state that I need is to run manage.py makemigrations [appname] for each of my apps. Shouldn't makemigrations, called without a specific app name, pick up all the installed apps and create their migrations? Where am I going wrong?
You're going wrong at the very end -- yes, you do need to call manage.py makemigrations <appname> for each of your apps once. It's not automatically done for all apps.
Presumably that is because Django has no way of knowing if that is what you want to do (especially if some apps were downloaded from PyPI, etc). And a single command per app can't really be an extreme amount of work, right?

Django 1.7 - makemigrations not detecting changes

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.
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.
This may happen due to the following reasons:
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
You don't have migrations folder inside those apps. (Solution: just create that folder).
You don't have __init__.py file inside migrations folder of those apps. (Solution: Just create an empty file with name __init__.py)
You don't have an __init__.py file inside the app folder. (Solution: Just create an empty file with name __init__.py)
You don't have a models.py file in the app
Your Python class (supposed to be a model) in models.py doesn't inherit django.db.models.Model
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
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)
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.
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.
Remove method with similar name as attribute you are adding.
manage.py makemigrations my_app
manage.py migrate my_app
Add the methods back.
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 :-)
Maybe I am too late but did you try to have a migrations folder in your app with a __init__.py file in it?
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.
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.
Following worked for me:
Add the app name to settings.py
use 'python manage.py makemigrations'
use 'python manage.py migrate'
Worked for me: Python 3.4, Django 1.10
People like me who don't like migrations can use steps below.
Remove changes what you want to sync.
Run python manage.py makemigrations app_label for the initial migration.
Run python manage.py migrate for creating tables before you make changes.
Paste changes which you remove at first step.
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.
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).
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
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.
Make sure your model is not abstract. I actually made that mistake and it took a while, so I thought I'd post it.
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...
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
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
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()
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
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 :)
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:
I made .\manage.py makemigrations app
I executed .\manage.py migrate
I erased both tables of my models.py
I erased all reference to my tables from serializer and view class.
I executed step 1 and 2.
I retrieved my changes just in the models.py
I executed again step 5.
I restored all my changes.
If you're working with Pycharm, local history is very helpfull.
Maybe this will help someone.
I've deleted my models.py and expected makemigrations to create DeleteModel statements.
Remember to delete *.pyc files!
./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.
You may need to fake the initial migrations using the command below
python manage.py migrate --fake-initial
Remove changes what you want to sync.
Run python manage.py makemigrations app_label for the initial migration.
Run python manage.py migrate for creating tables before you make changes.
Paste changes which you remove at first step.
Run 2. and 3. steps
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!
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.
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.
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.

NoMigrations error in Django

I am building an app called 'competencies'. I made changes to models, migrated the app locally, and everything worked. This is my eighth migration on the app.
I have deployed the app on heroku, so I committed changes and pushed to heroku. I can see that the changes went through, because the new migrations appear in the heroku files. When I log in to heroku and try to migrate the competencies app, I get the following error:
NoMigrations: Application '<module 'django.contrib.admin' from '/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/__init__.py'>' has no migrations.
I have searched for this error, and I have not found anything meaningful. Can anyone suggest what I am doing wrong, or how to address the issue?
django.contrib.admin should not have migrations. Contrib packages are not south managed.
If you EVER ran python manage.py schemamigration django.contrib.auth --XXX on your local it would create the migrations folder in your local copy's install of the venv's django. However, this will never get transferred to heroku.
test something for me. create a new copy of your site on your local machine
new DB
new virtualenv
new folder w/ new clone of repo
try to run python manage.py migrate if you get the same error its b/c you broke your virtualenv with south.
Something else you could try IF your database models have not changed a bunch since the last working copy:
roll your models back to the last working configuration
delete EVERY app's migrations folder
truncate south_migrations table
run python manage.py schemamigration --initial X for each of your apps.
push and migrate --fake
redo your model changes
create migrations for the model changes
push and migrate regularly
I recently encountered this error after dumping a live database to a dev box for testing data migrations.
One of the dependencies was throwing this error (specifically taggit). I think that I have a different version of taggit on the dev box which does not have migrations, but the database I dumped had two migrations for taggit in south_migrationhistory.
I deleted the entries in south_migrationhistory for the problem app erroneously claiming NoMigrations and that solved my problem. Everything's running again.
Apart from many answers posted above, south.exceptions.NoMigrations is often exception after 2014 (Django 1.7) because of changed migrations directory. The directory is default for Django built in migrations.
For south migration, the directory is south_migrations. South>=1.0 can recognize this and automatically detect migrations. See details here on Django docs
In this case, update South:
pip install -U South
Or you can also specify migration directory on settings file (for every app installed).
I found an odd edge case, involving Kombu.
I'm maintaining a legacy Django 1.5 project on Ubuntu 14.04, and I previously had this setting to get Kombu to play nicely:
SOUTH_MIGRATION_MODULES = {
'kombu_transport_django': 'kombu.transport.django.south_migrations',
}
However, after I upgraded to Ubuntu 16, there were some minor tweaks in the Python stdlib that again broke Kombu. Upgrading Kombu was the only immediate solution, but that gave me another Kombu error similar to what OP found:
NoMigrations: Application '<module 'kombu.transport.django' from '/myproject/.env/local/lib/python2.7/site-packages/kombu/transport/django/__init__.pyc'>' has no migrations.
This error message is nearly worthless, but I eventually realized the problem was my SOUTH_MIGRATION_MODULES setting. It turns out, the old kombu.transport.django.south_migrations was removed, and South trying to import this missing migration directory is what was causing the error. The fix was to remove that line.
Try pip install --upgrade django
The NoMigrations error often shows up if you downgrade your Django version.
In my case, I had installed a package which automatically uninstalled my current Django version and installed a downgraded it.
Installing the updated version was a quick fix.
Also, you may want to check in your INSTALLED_APPS if you have included all of your apps including the 'django.contrib.auth' and 'django.contrib.admin'

Running South migrations for all apps

I've just begun using South and am still in the process of figuring it out. Let's say I have the initial migration script of a model. Then i go add a column to the model and create a migration script for it. I then add another column to another model and create another migration script for it. I'm creating the migration script by running ./manage.py schemamigration myappname --auto.
Let's say I have a server on which my project is deployed but it was based on the initial schema of the application but now it lags behind the repository by two migrations. I can bring it up to date by running ./manage.py migrate myappname. This would bring that app's models up to date by running the the new migrations if I'm correct but I would explicitly have to specify the app.
Does South allow you run all pending migrations for all apps in a Django project? If so how? I haven't able to find any thing in the docs about this.
Thanks a ton everyone.
To bring all apps up to date on all their migrations, run:
./manage.py migrate
Simple. :)
You can also try:
./manage.py syncdb --migrate
to migrate all applications which use south and sync the applications which do not.

Categories