Django Schema Evolution

I’ve ported my schema evolution work from my SoC project last summer to Django v0.96.   To use it, download the patch below, and run the following:

$ cd /<path_to_python_dir>/site-packages/django/
$ patch -p1 < ~/<download_dir>/django_schema_evolution-v096patch.txt

It should output the following:

patching file core/
patching file db/backends/mysql/
patching file db/backends/mysql/
patching file db/backends/postgresql/
patching file db/backends/postgresql/
patching file db/backends/sqlite3/
patching file db/backends/sqlite3/
patching file db/models/fields/
patching file db/models/

To use it:

$ cd /<path_to_project_dir>/
$ ./ sqlevolve <app_name>

It should output something like this:

ALTER TABLE `main_query` CHANGE COLUMN `accuracy` `accuracynew` numeric(10, 6) NULL;
ALTER TABLE `main_query` ADD COLUMN `price` varchar(256) NULL;

Assuming you have a model such as this:

class Query(models.Model):
    query = models.CharField(maxlength=256, blank=False)
    accuracynew = models.FloatField(max_digits=10, decimal_places=6, null=True, blank=True, aka='accuracy')
    price = models.CharField(maxlength=256, null=True, blank=True) # new column

Note the aka field where I changed the name of “accuracy” to “accuracynew”.

Source code:


Let me know if you find any bugs.

UPDATE: This project is now on Google Code:

15 Responses to “Django Schema Evolution”

  1. Jökull Says:

    This looks very promising. How does this compare to Rails’ Migration feature? Looks a lot smoother.

  2. Harish Mallipeddi Says:

    How to run custom SQL as part of a schema migration? For instance if I wanted to combine two columns into one column?

  3. David Grant Says:

    Is this going to be merged into Django proper?

  4. rtconner Says:

    Or is there any equavalent for this planned for Django root? does this patch work with .97-latest?

  5. derek Says:

    david: that’s my goal
    rtconner: yes to your second question

  6. buriy Says:

    Don’t work for postgres_psycopg2 backend! If you copy changes from postgres backend, don’t work also.
    File “D:\web\projects\ef\”, line 11, in ?
    File “d:\program\python24\lib\site-packages\django\core\”, line 1952, in execute_manager
    execute_from_command_line(action_mapping, argv)
    File “d:\program\python24\lib\site-packages\django\core\”, line 1843, in execute_from_command_line
    action_mapping[action](int(options.verbosity), options.interactive)
    File “d:\program\python24\lib\site-packages\django\core\”, line 793, in syncdb
    for sql in get_sql_evolution(app):
    File “d:\program\python24\lib\site-packages\django\core\”, line 546, in get_sql_evolution
    output = get_sql_evolution_check_for_dead_fields(klass, new_table_name)
    File “d:\program\python24\lib\site-packages\django\core\”, line 698, in get_sql_evolution_check_for_dead_fields
    TypeError: iteration over non-sequence

  7. derek Says:

    buriy: fixed this in the schema-evolution branch. (i assume this is where you ran across it, not in the v0.96 patch, right?)

  8. Mike Heald Says:

    Looks like excellent stuff! Looking closer at what you’re doing here, it looks like our migration projects would actually work very nicely together! A situation where individual developers in a team could use evolution to generate the migrations they need, review them, and then use dbmigration to automatically roll them out to demo/UAT/live servers would be my ideal way of doing things.

  9. derek Says:

    mike, i couldn’t agree more. :)

  10. buriy Says:

    Please, patch for django-trunk.
    By the way, works perfectly for django-newforms admin!

  11. Zarek Says:

    This code is very interesting for creating an audit trail for a given model.

  12. » Actualización de Estado de Django: 22 de Julio - Blog de Marcelo Ramos Says:

    [...] Aparentemente pisándole los talones al proyecto de Mike, Derek Anderson, el autor del proyecto schema-evolution del Summer of Code 2006, ha actualizado su código para que funcione adecuadamente con la versión 0.96 de Django. Derek afirma que el schema-evolution también debería funcionar correctamente con la versión trunk de Django . [...]

  13. krl Says:

    I don’t know if you are interested guys but there is an interesting new approach to schema evolution, is called PRISM, developed at UCLA is a tool to support schema evolution in relational DB.

    there is a video of th interface here:

    while from here: you can access the on-line demo.

  14. SQL Tutorials Says:

    You know, the thing about SQL is, that there is virtually nothing that can replace it.

    Does anyone know if a substitute exists for sql? I mean besides MS SQL and Oracle and all that jazz. Thanks.

  15. Carroll B. Merriman Says:

    Thanks for your article. I am new at django and this will be a big help.

Leave a Reply

<>   © Copyright 2000-2005 by Derek Anderson
Get Firefox