How to recover South from flushing db - django

I bulk loaded some data into my Postgresql db, only to realize all of it was a bit wrong. Keen to rectify this I flushed the db in order to reload the correct data.
Data reloaded, I made a change to a model and applied the normal ./manage.py schemamigration app --auto, and then ./manage.py migrate app (which had been working up to this point).
At this point I started getting the error:
django.db.utils.DatabaseError: relation "app_model" already exists
And the traceback appeared to be calling the initial migration file 001. So I think the answer is to do something like:
./manage.py migrate app 0005 --fake
Where the current migration that I'm looking to apply is 0006 (i.e. this is where the migrations began failing), and then the south database within postgres should be back where it was before I foolishly flushed it. At this point I should be able to
./manage.py migrate app
Can someone tell me that this approach is correct? Or if not, what the correct approach is?

After python manage.py migrate app --fake
do a python manage.py schemamigration app --auto
and then do a python manage.py migrate app

You can also make it more general:
python manage.py migrate
python manage.py flush
python manage.py migrate --fake
This will recover all the south history after flush

Related

Cannot get Django 1.7 Migrations to detect proper changes to my DB.

I have a production web project running with a decent amount of data in the MySQL db. I am trying to update the database with some changes to an app called "enterlink." I've made new elements in the existing models and created new models altogether. Before this migration, I have never touched the schema of the db since originally running syncdb to create it. When I run: "python manage.py makemigrations enterlink" the below output appears(pic). My question is, why is this happening? The DB already includes all the models that it lists in the picture so why is it registering those lists of models? When I go to finish the migration by doing "python manage.py migrate" or "python manage.py migrate --fake enterlink" (pic again), I get an output shown but my database schema remains identical to the old db and any new code generates errors. Can anyone tell me what is likely the problem? I would be really appreciative of any advice. It's been very frustrating since I'm not sure what I'm missing.
What you have done is that you have ran the command python manage.py syncdb before running python manage.py makemigrations myapp and python manage.py migrate myapp. That is why syncdb created the database schema and the migration was faked because schema already exists. I will suggest to use python manage.py makemigrations myapp and python manage.py migrate myapp and not to use syncdb as its deprecated in Django 1.7.
If you change anything in your model, just run makemigrations and migrate command. Syncdb isn't necessary.
This question and relevant answers are intriguing me. Thus I want to share my experience on maintaining live database and migrations.
Tested in django1.5.5
Initializing the database:
./manage.py syncdb --noinput
./manage.py migrate
./manage.py syncdb
Now I have created the database.
Doing a migration for an app:
./manage.py schemamigration myapp --initial
./manage.py migrate myapp --fake
Now do necessary changes in your model
./manage.py schemamigration myapp --auto
./manage.py migrate myapp
Im newbie for schemamigration too, but i will explain how it works for me:
First you create app and then
./manage.py sycndb, so tables are created then you can
./manage.py makemigrations myapp --initial
so now initial migrations are created and you should apply them
./manage.py migrate myapp
now you can change your models : add,change fields, anything you want and then
./manage.py makemigrations myapp --auto
this will create migrations for changes and now you need to apply them
enter code here./manage.py migrate myapp
so this actually will create new tables in db

Django - Deploy syncdb & South

I'm deploying a project on a new development environment.
As I'm using South I did:
$ python manage.py syncdb --all
$ python manage.py migrate --fake
I used syncdb --all to apply actual state of models.
Then migrate --fake to mark all models as migrated.
But after that, my model is not on the last version (missing fields)
What am I doing wrong ?
I assume all my modifications have migrations.
If I do
$ python manage.py syncdb
It seems to create the first state since when I used South (that is expected)
But then
$ python manage.py migrate
Some tables appears as already created
Actually this, should have been fine for my case
$ python manage.py syncdb --all
$ python manage.py migrate --fake
Having to redeploy my app recently, I faced the same issue.
I just realized that I had a double initial migration on the model that were causing the problem
0001_initial.py
0002_initial.py
0003_auto__add_field_mytable_myfield.py
I simply deleted & renamed
0001_initial.py
0002_auto__add_field_mytable_myfield.py
Then redone the whole database deployment (obviously not forgetting to update the already applied migrations on my other hosts)
--fake option does not avoid errors while trying to create new migrations. It records that migrations have applied without actually applying them.
Also, you need --ignore-ghost-migrations or --delete-ghost-migrations to achieve what you are looking for.
To convert an existing project to south, you first need to convert the app
Now, if you have already run --fake, to recover, you can do this:
Go to ./manage.py dbshell
DELETE FROM south_migrationhistory WHERE id > 0; //Note that this would delete everything in the table.
If you want to remove migrations of a specific app,
DELETE FROM south_migrationhistory WHERE app_name = 'blah'

South - Not detecting changes to Django model

I have Django model that I already have initialized with south using ./manage.py schemamigration (appname) --initial. All was going well through this point, until I decided I needed another field. I added another field and tried to to migrate the change with ./manage.py schemamigration (appname) --auto, but it says:
- Nothing to migrate.
I made sure to migrate the initial changes.
Seems to be similar to the problem here, but the solution got me nowhere.
It's probably because between the --initial migration and the next schemamigration you have to persist the actual migration to the db issuing the command python manage.py migrate my_app.
After doing that first migration then you may add another field, do and schemamigration --auto and commit it to the db again by doing python manage.py migrate my_app
Hope this helps!

How to do a syncdb with django, when migrations are also involved

when I do a syncdb I get the following error everytime:
Not synced (use migrations):
- deals
- analytics
(use ./manage.py migrate to migrate these)
And when I run sudo python manage.py migrate. I get the following
Running migrations for deals:
- Nothing to migrate.
- Loading initial data for deals.
No fixtures found.
Running migrations for analytics:
- Nothing to migrate.
- Loading initial data for analytics.
No fixtures found.
I highly appreciate your help
From the output, it seems like the database is already synchronized with the migrations. There are no problematic errors. (Although you shouldn't really be root to run the migrations.)
If you're looking into creating more migrations, use the south documentation, which usually is just running the following after you modify the models:
python manage.py schemamigration --auto <APP>
And then use python manage.py migrate to apply the changes.
It looks like migrations have been already passed. Check south_migationhistory table in db.
If you want to sync new db for apps which has migrations just disable south in settings.py.
Have you ran a schemamigration initial yet?
./manage.py schemamigration deals --initial
./manage.py migrate deals
if you get the error, db already excists do this:
./manage.py schemamigration deals --initial
./manage.py migrate deals --fake

south django migrate

I just did:
python manage.py schemamigration TestDBapp1 --initial
python manage.py schemamigration TestDBapp1 --auto
Successfully.
But if I enter: python manage.py migrate TestDBapp1
I get this: sqlite3.OperationalError: table "TestDBapp1_xyz" already exists
What could be the problem?
I suspect that you already executed syncdb which created the tables. South tries to create them again during migrate and naturally the database complains.
To avoid this you have to tell South to "fake" the initial migration.
python manage.py migrate TestDBapp1 --fake
As the name indicates this pretends to migrate. Note that this is an one time step. South will handle your future syncdb and migrate without requiring --fake.