<p>I'm considering using Django for a project I'm starting (fyi, a browser-based game) and one of the features I'm liking the most is using <code>syncdb</code> to automatically create the database tables based on the Django models I define (a feature that I can't seem to find in any other framework).
I was already thinking this was too good to be true when I saw this in the <a href="https://docs.djangoproject.com/en/dev/ref/django-admin/" rel="noreferrer">documentation</a>:</p>
<blockquote>
<p>Syncdb will not alter existing tables</p>
<p>syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.</p>
<p>If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.</p>
</blockquote>
<p>It seems that altering existing tables will have to be done &quot;by hand&quot;.</p>
<p>What I would like to know is the best way to do this. Two solutions come to mind:</p>
<ul>
<li>As the documentation suggests, make the changes manually in the DB;</li>
<li>Do a backup of the database, wipe it, create the tables again (with syncdb, since now it's creating the tables from scratch) and import the backed-up data (this might take too long if the database is big)</li>
</ul>
<p>Any ideas?</p>

[{'answer_id': 36013, 'author': 'Carl Meyer', 'author_id': 3207, 'author_profile': 'https://Stackoverflow.com/users/3207', 'pm_score': 6, 'selected': False, 'text': '<p>Manually doing the SQL changes and dump/reload are both options, but you may also want to check out some of the schema-evolution packages for Django.  The most mature options are <a href="http://code.google.com/p/django-evolution/" rel="noreferrer">django-evolution</a> and <a href="http://south.aeracode.org/" rel="noreferrer">South</a>.</p>\n\n<p><strong>EDIT</strong>: And hey, here comes <a href="https://code.google.com/p/dmigrations/" rel="noreferrer">dmigrations</a>.</p>\n\n<p><strong>UPDATE</strong>: Since this answer was originally written, <a href="http://code.google.com/p/django-evolution/" rel="noreferrer">django-evolution</a> and <a href="https://code.google.com/p/dmigrations/" rel="noreferrer">dmigrations</a> have both ceased active development and <a href="http://south.aeracode.org/" rel="noreferrer">South</a> has become the de-facto standard for schema migration in Django. Parts of South may even be integrated into Django within the next release or two.</p>\n\n<p><strong>UPDATE</strong>: A schema-migrations framework based on South (and authored by Andrew Godwin, author of South) is included in Django 1.7+.</p>\n'}, {'answer_id': 36015, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 3, 'selected': False, 'text': "<p>One good way to do this is via fixtures, particularly the <code>initial_data</code> fixtures.</p>\n\n<p>A fixture is a collection of files that contain the serialized contents of the database.  So it's like having a backup of the database but as it's something Django is aware of it's easier to use and will have additional benefits when you come to do things like unit testing.</p>\n\n<p>You can create a fixture from the data currently in your DB using <code>django-admin.py dumpdata</code>.  By default the data is in JSON format, but other options such as XML are available.  A good place to store fixtures is a <code>fixtures</code> sub-directory of your application directories.</p>\n\n<p>You can load a fixure using <code>django-admin.py loaddata</code> but more significantly, if your fixture has a name like <code>initial_data.json</code> it will be automatically loaded when you do a <code>syncdb</code>, saving the trouble of importing it yourself.</p>\n\n<p>Another benefit is that when you run <code>manage.py test</code> to run your Unit Tests the temporary test database will also have the Initial Data Fixture loaded.</p>\n\n<p>Of course, this will work when when you're adding attributes to models and columns to the DB.  If you drop a column from the Database you'll need to update your fixture to remove the data for that column which might not be straightforward.</p>\n\n<p>This works best when doing lots of little database changes during development.  For updating production DBs a manually generated SQL script can often work best.</p>\n"}, {'answer_id': 55716, 'author': 'Garth Kidd', 'author_id': 5700, 'author_profile': 'https://Stackoverflow.com/users/5700', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve been using django-evolution. Caveats include: </p>\n\n<ul>\n<li>Its automatic suggestions have been uniformly rotten; and</li>\n<li>Its fingerprint function returns different values for the same database on different platforms. </li>\n</ul>\n\n<p>That said, I find the custom <code>schema_evolution.py</code> approach handy. To work around the fingerprint problem, I suggest code like:</p>\n\n<pre><code>BEFORE = \'fv1:-436177719\' # first fingerprint\nBEFORE64 = \'fv1:-108578349625146375\' # same, but on 64-bit Linux\nAFTER = \'fv1:-2132605944\' \nAFTER64 = \'fv1:-3559032165562222486\'\n\nfingerprints = [\n    BEFORE, AFTER,\n    BEFORE64, AFTER64,\n    ]\n\nCHANGESQL = """\n    /* put your SQL code to make the changes here */\n    """\n\nevolutions = [\n    ((BEFORE, AFTER), CHANGESQL),\n    ((BEFORE64, AFTER64), CHANGESQL)\n    ]\n</code></pre>\n\n<p>If I had more fingerprints and changes, I\'d re-factor it. Until then, making it cleaner would be stealing development time from something else. </p>\n\n<p><strong>EDIT:</strong> Given that I\'m manually constructing my changes anyway, I\'ll try <a href="http://simonwillison.net/2008/Sep/3/dmigrations/" rel="nofollow noreferrer">dmigrations</a> next time. </p>\n'}, {'answer_id': 57908, 'author': 'defrex', 'author_id': 6007, 'author_profile': 'https://Stackoverflow.com/users/6007', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://code.google.com/p/django-command-extensions/" rel="nofollow noreferrer">django-command-extensions</a> is a django library that gives some extra commands to manage.py. One of them is sqldiff, which should give you the sql needed to update to your new model. It is, however, listed as \'very experimental\'.</p>\n'}, {'answer_id': 186996, 'author': 'akaihola', 'author_id': 15770, 'author_profile': 'https://Stackoverflow.com/users/15770', 'pm_score': 5, 'selected': True, 'text': '<p>As noted in other answers to the same topic, be sure to watch the <a href="http://www.youtube.com/watch?v=VSq8m00p1FM" rel="noreferrer">DjangoCon 2008 Schema Evolution Panel</a> on YouTube.</p>\n\n<p>Also, two new projects on the map: <a href="http://github.com/ricardochimal/simplemigrations/tree/master" rel="noreferrer">Simplemigrations</a> and <a href="http://www.bitbucket.org/DeadWisdom/migratory/" rel="noreferrer">Migratory</a>.</p>\n'}, {'answer_id': 411051, 'author': 'max', 'author_id': 49407, 'author_profile': 'https://Stackoverflow.com/users/49407', 'pm_score': 2, 'selected': False, 'text': '<p>So far in my company we have used the manual approach. What works best for you depends very much on your development style.</p>\n\n<p>We generally have not so many schema changes in production systems and somewhat formalized rollouts from development to production servers. Whenever we roll out (10-20 times a year) we do a fill diff of the current and the upcoming production branch reviewing all the code and noting what has to be changed on the production server. The required changes might be additional dependencies, changes to the settings file and changes to the database.</p>\n\n<p>This works very well for us. Having it all automated is a niche vision but to difficult for us - maybe we could manage migrations but we still would need to handle additional library, server, whatever dependencies.</p>\n'}, {'answer_id': 20640720, 'author': 'Jan Wrobel', 'author_id': 1031601, 'author_profile': 'https://Stackoverflow.com/users/1031601', 'pm_score': 2, 'selected': False, 'text': '<p>Django 1.7 (currently in development) is <a href="https://docs.djangoproject.com/en/dev/releases/1.7/#what-s-new-in-django-1-7" rel="nofollow">adding native support</a> for schema migration with <code>manage.py migrate</code> and <code>manage.py makemigrations</code> (<code>migrate</code> deprecates <code>syncdb</code>). </p>\n'}]
['https://Stackoverflow.com/questions/35991', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2841/']