django south migration - app migration is not in south_migration table - django

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

Related

django - schema migration - how to add a field

I have a django 1.8 app working with a db.
I'm trying to change the schema of a table using the built-in migration.
Here are the steps I did:
In my dev invironment, I grabbed the app source and ran
python manage.py sycdb
then I ran
python manage.py loaddata ~/my_data.json
then I modified modes.py. Added a field and renamed a field...all from the same table 'TABLE1' which had no data.
then
python manage.py makemigrations myapp
python manage.py migrate
Error: django.db.utils.OperationalError: table "myapp_someother_table" already exists
then ran
python manage.py migrate --fake-initial
worked!
but when I browsed to the admin page for TABLE1, I get this error:
OperationalError: no such column: myapp_table1.my_new_field_id
I checked the db and yes, there is no such column.
How can I procceed from here? I prefer to fix this via django.
If I fix it straight in the db, then the migration goes out of sync.
Migrations do not automagically see that you have made changes. Migrations detect changes by comparing the current model with the historical model saved in the migration files.
In this case, you didn't have any historical models, since you didn't have any migrations. Django was not able to detect any changes in your models, even though they were different from your database.
The correct way to make changes to your model is to first run manage.py makemigration <my_app>, and then make the changes to your model, followed by another manage.py makemigrations.
You might not be able to do it via pure django and keep your data. I don't have personal experience with south but there are a lot of mentions if this tool. Just in case if nothing else works for you...
Here is what I did to make things work, but there must be a better way so please add more answers/comments...
I deleted the sqlite db and the migration folder
I made the desired changes to model.py
ran syncdb
ran loaddata to load the json data dump that I had saved previously.
just started the dev server

Broken makemigrations in django

I added I new app in django project. After that I made a migration and migrate on it. Then I changed some field in model, and after trying to create some instance it was mistake with this field. So after that I made I terrible mistake and delete all migrations in this app, and now I can't make new migrations.
I have such error: "Migration ... dependences reference nonexistent parent node" some where in old app. But this migration is exist.
If you need to delete a migration you should run reverse migrations before deleting a migration. So if you're on migration 0004 and you made a mistake in that and wanted to go back, run the reverse to the previous migration e.g.
python manage.py migrate myapp 0003
This brings the database tables for myapp to the state they were in for migration 0003.
If you wanted to correct something, or re-create an initial migration for a feature you're making and you don't need to keep the test data you've created you should run;
python manage.py migrate myapp zero
This will run the backwards migration on all migrations for myapp including the initial migration.
If you've deleted all your migration files and you now want to do something with migrations for that app you'll have to delete the tables in the database relating to your app, then remove the rows from the migrations table (django_migrations) which relate to any migrations from your app and simply recreate the initial migration again.
The rows in the migrations table looks like this;
1 contenttypes 0001_initial 2015-04-16 10:09:38
So just look for your app name in that table.

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.

What is the correct way to deal with DB migration while using South, Django and Git?

Background :-
I am using Django 1.3. We are using South as the module for DB migration and Git SCM.
Problem:-
What is the correct way to deal with the migrations Folder that is formed?
The main problem is I make changes in the DB schema in the development machine, when I upload it to the production server I have to migrate the existing schema. While doing that there is always some issue with the migration files.
Should I just add the migrations folder to the gitignore ? or is there a better way to go about it ?
You should add the migrations folder to your version control system and use the same files for production and development. You may run into some problems on your production system if you introduced your migrations not from the beginning and you have already existing tables.
Therefore you have to fake the first migration, which normally does the same thing as syncdb did when you created your database for the first time.
So when trying to apply migrations for your app for the first time on the production machine, execute manage.py migrate app_name 0001 --fake. This lets South know, that the first migration has already been applied (which already happend with syncdb) and when you run migrate again, it would continue with the following migrations.

newbie difficulty using south with pycharm - DatabaseError: no such table: south_migrationhistory

I'm using sqlite3 and pycharm to learn more about django, and googled to find that south is recommended to make it easier to modify models after they have been created.
I'm trying to follow the advice on http://south.aeracode.org/docs/tutorial/part1.html#starting-off.
The most success I've had so far is to create a simple model and run syncdb before adding south to installed_apps. That way the intial tables are created and I get a chance to create a super user. (Django admin seems to fret if there are no users).
Then I add south to installed_apps, and run django_manage.py schemamigration bookmarks --initial
It seems to work fine. A new directory is created called migrations with a couple of files in it in my app folder and an encouraging message.
"Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate bookmarks"
The next step - django_manage.py" migrate bookmarks generates the following error message
django.db.utils.DatabaseError: no such table: south_migrationhistory.
I thought that table would be created in the first schememigration step. What am I missing? Can anyone help?
Marg
South uses a table if its own to keep track of which migrations have been applied. Before you can apply any migrations, this must have been created, using python ./manage.py syncdb.
As well as for setting up south, you will find syncdb sometimes necessary for non-south apps in your project, such as the very common django.contrib.auth.
Note that as a convenience, you can run both in one go like this
python ./manage.py syncdb --migrate
My latest (unsuccessful) effort was the following
Create application – synch db – superuser created
Test run –admin screen shows basic tables
Add south, and syncdb from command line with manage.py syncdb – south_migrationhistory table created. Add basic vanilla model
Tried various combinations of manage.py syncdb –manage, and
schemamigration from Pycharm (if run from within pycharm a
migrations directory is created within the app
– if run from the command line the directory does not seem to be
created.)
Django admin screen shows table – but if I try to edit
the table it says that it doesn’t exist
Check database structure
using SQLite browser - table for newly created model doesn’t exist
I’m starting to think that the whole thing is not worth the time wasting hassle – maybe I’m better off just modifying the tables in SQLite browser
Answer in the similar question:
Run syncdb to add the Django and South tables to the database.