Django database - how to add this column in raw SQL
Suppose I have my models set up already.
class books(models.Model):
title = models.CharField..开发者_JAVA百科.
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?
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`);
精彩评论