Migrations
Since Django 1.7
New In Django 1.7
Commands
Makemigrations
python manage.py makemigrationsMigrate
python manage.py migrateForwards/Backwards
Database and
Django_migrationsBoth Changepython manage.py migrate app_name 0001Database Not and
Django_migrationsChangepython manage.py migrate app_name 0001 --fake
Problems
ValueError: Cannot serialize:
Models
uid = models.CharField(_(u'uid'), max_length=255, default=shortuuid.uuid, help_text=u'User UUID', db_index=True)Description
ValueError: Cannot serialize: <bound method ShortUUID.uuid of <shortuuid.main.ShortUUID object at 0x1025eab90>> There are some values Django cannot serialize into migration files. For more, see https://docs.djangoproject.com/en/1.8/topics/migrations/#migration-serializingSolution
Use ShortUUIDField
Installation
pip install django-shortuuidfieldUsage
from shortuuidfield import ShortUUIDField uid = ShortUUIDField(_(u'uid'), max_length=255, help_text=u'User UUID', db_index=True)
No changes detected
python manage.py makemigrations-No changes detectedpython manage.py makemigrations app_name-Changes detected
South
Conflict
There is no South database module 'south.db.mysql' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS.
Before Django 1.7
South
Install
pip install south
Commands
Initial
python manage.py schemamigration app_name --initialAdd-On Field
python manage.py schemamigration app_name --autoExists Projects Convert to Use South
python manage.py syncdb # Syncdb has already changed by South, to create table ``south_migrationhistory`` in Database python manage.py convert_to_south app_name # create 0001_initial.py, insert record in table ``south_migrationhistory``Forwards/Backwards
Database and
South_migrationhistoryBoth Changepython manage.py migrate app_name 0001Database Not and
South_migrationhistoryChangepython manage.py migrate app_name 0001 --fake
Problems
Multiple Branches
Suppose app_name developed in two branches(master and branch2) from 0004
At the time of merge, mater is 0007 and branch2 is 0008, and they don't add the same field
Solve multiple branches merge
Exec command in master branch
git merge branch2Solve conflict
south_migrationhistory back to 004
python manage.py migrate app_name 0004 --fakedelete migrations after 0004
create new migration
python manage.py schemamigration app_name --autosouth_migrationhistory back to 0005
python manage.py migrate app_name 0005 --fake
Problems
- Problem after Migrate
django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xE6\\x95\\xB0\\xE6\\x8D\\xAE...' for column 'name' at row 1")
References
[1] ferprez@StackOverflow, There is no South database module 'south.db.postgresql_psycopg2' for your database