views:

72

answers:

2

Hi all,

I am preferring to manually migrate my tables in Django. Because using automated tools puts me in a place where I cannot see the impact. With impact, I mean the the time it takes the db get in synch with my models. Below is a simple example:

class User(models.Model):
   first_name = CharField(..)

Let's say I want to add this:

class User(models.Model):
       first_name = CharField(..)
       last_name = CharField(..)

I will follow the these steps in my production server:

  1. Disable site traffic.
  2. Manually connect to the your DB server, let's say MySQL and add a field to the User table named last_name (make sure it is sync with the SQL generated for the new Model, of course.)
  3. Update your model.
  4. Upload new files, restart traffic.

I have two questions for this scenario:

  1. Is this a preferred/acceptable way for manual db migration in Django?
  2. If I just add a field with a specific default value to the User table by SQL manually, but don't update the model, will I still get DatabaseIntegrity exception?

Thanks in advance,

+3  A: 

With all of the schema migration tools, such as south, there are ways of explicitly defining how your models get migrated. The benefits of using a tool such as this are:

  • Your migrations are stored in your version control system
  • There's a documented procedure to roll back schema migrations
  • If another developer joins your project, you can refer that person to the south documentation rather than explaining your own hacky solution to documenting schema migrations.

I think I should just emphasize a point here: Though south has automigration tools, you don't have to use automigration if you're using South.

Mike Axiak
+1 Use South...
hughdbrown
A: 

Is this a preferred/acceptable way for manual db migration in Django?

I would answer no. As @Mike said Django has a reliable and fairly versatile ecosystem of migration tools, the most prominent of which is South. @Mike's answer has the details right.

To answer your second question:

If I just add a field with a specific default value to the User table by SQL manually, but don't update the model, will I still get DatabaseIntegrity exception?

No. Your models will continue to function normally. Of course if you want to do something with the new fields using Django's ORM you'll be better off adding them to the model class.

A side effect of this is that you can migrate legacy database tables by selectively choosing the fields to use in your models.

Manoj Govindan