views:

47

answers:

1

Suppose I have my models set up already.

class books(models.Model):
    title = models.CharField...
    ISBN = models.Integer...

What if I want to add this column to my table?

user = models.ForeignKey(User, unique=True)

How would I write the raw SQL in my database so that this column works?

+1  A: 

You should investigate a tool like South, which does all this for you.

However the SQL would be something like (assuming you're using MySQL):

ALTER TABLE `appname_books` ADD COLUMN `user_id` INTEGER NOT NULL UNIQUE;
ALTER TABLE `appname_books` ADD CONSTRAINT `user_id_refs_user` FOREIGN KEY (`user_id`) REFERENCES auth_user (`id`);
Daniel Roseman
If I already have running data, and a set-up database....can I use South right now? Or will I have to re-create all the tables?
TIMEX
Anyone who comes along and sees alex's comment, this is addressed in his later question (http://stackoverflow.com/questions/2445761/django-migrations-is-it-possible-to-use-south-in-the-middle-of-the-project).
Dominic Rodger